Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

A federal judge Tuesday blunted the impact of one of President Donald Trump's executive orders on immigration, forbidding the White House from withholding federal funds from sanctuary cities — local governments that limit police cooperation with federal immigration authorities.

Federal District Court Judge William Orrick issued a nationwide injunction in response to a lawsuit filed by San Francisco and nearby Santa Clara County. They argued that the president's January 25th executive order, declaring sanctuary cities ineligible to receive federal grants, was unconstitutional.

"The Constitution vests the spending power in Congress, not the President," the judge said. President Trump's executive order "cannot constitutionally place new conditions on federal funds."

San Francisco's city attorney, Dennis Herrera, applauded the judge's order. "This is why we have courts - to halt the overreach of a president and an attorney general who either don't understand the Constitution or chose to ignore it."

Related: San Francisco Seeks Ban on Trump ‘Sanctuary Cities’ Order

The Trump administration argued that the lawsuit was premature because the government hasn't yet taken steps to carry out the executive order. But the judge said the city and county were already feeling the effects of budget uncertainty caused by the administration's threat to withhold grants. Lawyers for San Francisco said the city could lose about $2 billion a year.

Tuesday's order covered a section of Mr. Trump's executive order titled "Enhancing Public Safety in the Interior of the United States." The challenged provision directed the attorney general to ensure that sanctuary cities "are not eligible to receive federal grants." It also directed the secretary of Homeland Security to designate local governments as sanctuary cities.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

At the heart of the dispute are detainer requests, issued by US Immigration and Customs Enforcement or ICE, asking local police and sheriff's offices to hold jail inmates for up to 48 hours after they have completed serving their sentences. The requests apply to people in the US illegally who are convicted of committing local crimes and who are subject to deportation after they are released.

City ordinances prohibit San Francisco police from giving immigration authorities advance notice of an inmate's release from jail, even if the police know that ICE wants to detain that person.

Related: Conservative Cities See 'Sanctuary City' Term as Scarlet Letter

Judge Orrick also said the executive order was unconstitutional because it imposed conditions on federal funds already issued or approved. And he said the president's action was too broad.

"Federal funding that bears no meaningful relationship to immigration enforcement cannot be threatened merely because a jurisdiction chooses an immigration enforcement strategy of which the president disapproves," the judge wrote.

Santa Clara Supervisor Cindy Chavez in a statement Tuesday said the Trump administration attempted an "end run" around the Constitution. Board of Supervisors President Dave Cortese said “The politics of fear emanating from the Trump White House has just suffered a major setback.”

"The court’s decision is a win for the neediest people in our nation. Seniors in need of food, foster youth in need of shelter and children who need medical care,” Chavez said. "We’ll continue being a welcoming, safe and diverse community."

Tuesday's order does not completely forbid the federal government to withhold grant money from uncooperative cities. An existing federal law says local governments cannot restrict law enforcement officials from sending information to the federal government about the citizenship status of any person.

The judge said his injunction "does not impact the government's ability to use lawful means to enforce existing conditions of federal grants," including obeying that law.

In a statement, the Justice Department said Tuesday's order upheld the government's ability to enforce existing conditions on federal grants and said it "will follow the law with respect to regulation of sanctuary jurisdictions."

White House Chief of Staff Reince Priebus said Tuesday night that the administration plans to appeal. "The idea that an agency can’t put in some reasonable restrictions on how some of these monies are spent is something that will be overturned eventually," Priebus said. "We'll win at the Supreme Court at some point."

Similar lawsuits are pending in Washington, Massachusetts, and elsewhere in California.

New York City Mayor Bill de Blasio said in a statement Tuesday that the judge’s order is another defeat for the Trump administration’s policies on immigration.

"Once again a federal court has told the Trump administration: 'No you can't.' The president is going beyond his authority when he tries to cut vital funding to cities that don’t share his illogical and unconstitutional desire to scapegoat immigrants," de Blasio, a Democrat, said.