A federal district judge on Wednesday ruled against the state of Texas and halted major provisions of a controversial state-based immigration enforcement law just days before it was scheduled to go into effect.

U.S. District Judge Orlando Garcia granted a preliminary injunction of Senate Bill 4, one of Gov. Greg Abbott’s key legislative priorities that seeks to outlaw “sanctuary” entities, the common term for governments that don’t enforce federal immigration laws.

As passed, SB 4 allows local law enforcement officers to question the immigration status of people they detain or arrest and seeks to punish local government department heads and elected officials who don’t cooperate with federal immigration "detainers" — requests by agents to turn over immigrants subject to possible deportation. Punishment could come in the form of jail time and penalties that exceed $25,000.

Garcia halted the part of the bill that required jail officials to honor all detainers, and another that prohibits “a pattern or practice that 'materially limits' the enforcement of immigration laws.”

The Texas Tribune thanks its sponsors. Become one .

The detainer provision, he said, would violate the Fourth Amendment

Garcia did let stand one of the most controversial portions of the law — allowing police officers to question the immigration status of people they detain.

Because the inquiry into status isn’t a prolonged detention, he said, it wasn’t enjoined. But he explained that officers who make the inquiry are limited in what they can do with the information.

“If during a lawful detention or arrest an officer obtains information that a detained or arrested individual is undocumented he may not arrest the individual on this basis,” he said, adding that the officer is not required to ask the question. But he said if the officer feels like they should, they can only share the information.

“In sum, SB 4 gives local officers discretion to inquire and share information, but it does not provide them with discretion to act upon the information that they may obtain,” he wrote in a footnote to his 94-page ruling.

The bill was scheduled to go into effect Sept. 1, but opponents of the legislation, including the cities of Houston, Austin, San Antonio and El Cenizo, as well as Maverick and El Paso counties and the Mexican American Legal Defense and Educational Fund, argued the bill violates several provisions of the Constitution. Garcia's decision means the bill is on hold until that issue is decided or until the preliminary injunction is appealed.

The Texas Tribune thanks its sponsors. Become one .

His decision is a temporary but significant blow to Abbott and other Republican backers of the bill who said it would help keep Texans safe from undocumented immigrants that have been arrested on criminal charges but released from custody by sheriffs or other elected officials who refuse to hold the alleged criminals for possible deportation.

Abbott on Wednesday night promised an immediate appeal.

“Today’s decision makes Texas’ communities less safe,” he said in a statement. “Because of this ruling, gang members and dangerous criminals, like those who have been released by the Travis County Sheriff, will be set free to prey upon our communities. U.S. Supreme Court precedent for laws similar to Texas’ law are firmly on our side. This decision will be appealed immediately and I am confident Texas' law will be found constitutional and ultimately be upheld.”

Texas Attorney General Ken Paxton added: "Texas has the sovereign authority and responsibility to protect the safety and welfare of its citizens. We’re confident SB 4 will ultimately be upheld as constitutional and lawful.”

In his ruling, Garcia also noted the injunction would serve the public and cited the overwhelming opposition to the bill during public testimony at the Capitol.

"The public interest in protecting constitutional rights, maintaining trust in local law enforcement and avoiding the heavy burdens that SB4 imposes on local entities will be served by enjoining these portions of SB4," he said.

In another footnote, Garcia said that placing the law on hold would also benefit the state due to the sheer number of subsequent lawsuits that would likely follow if the legislation stood.

"If SB4 is implemented the state will begin spending public funds to enforce SB4 against local entities that will also spend public funds to defend themselves," he said. "Both state and local entities will also need to expend public funds to defend against spin off litigation."

The Texas Tribune thanks its sponsors. Become one .

Opponents of the law cheered the injunction.

"The court was right to strike down virtually all of this patently unconstitutional law. Senate Bill 4 would have led to rampant discrimination and made communities less safe," said Terri Burke, the executive director of the ACLU of Texas. "That’s why police chiefs and mayors themselves were among its harshest critics — they recognized it would harm, not help, their communities."