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

Public employees can sue, claiming their civil rights were violated, as long as their employers thought a constitutional right was in play, the U.S. Supreme Court ruled Tuesday.

The decision was a victory for a New Jersey man, Jeffrey Heffernan, who was a police officer in Patterson, New Jersey, when the mayor was running for re-election.

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

Heffernan was demoted after city officials mistakenly assumed he was campaigning for a candidate who was running against the mayor.

The officials thought he went to the campaign headquarters of the mayor's opponent to pick up and later display a campaign sign. But Heffernan said he went there to get the sign for his bedridden mother and had no intention of campaigning for the opponent.

A view of the U.S. Supreme Court building is seen in Washington, in this October 13, 2015 file photo. JONATHAN ERNST / Reuters

Heffernan sued, claiming he was punished for engaging in constitutionally protected speech. The city said, what speech? If he wasn't campaigning, he wasn't doing anything protected by the Constitution. The lower courts agreed with the city.

But by a 6-2 vote, the Supreme Court reversed those rulings.

Justice Stephen Breyer's opinion for the court said civil rights protections apply "to any employer who punishes an employee because the employer believes the employee has engaged in conduct that the First Amendment protects — even if the employer is factually mistaken about the conduct."

What counts, the court said, is the employer's motive, because the First Amendment focuses on government actions.

Justices Clarence Thomas and Samuel Alito dissented. They said the law does not protect someone whose constitutional rights have not been violated. They city's action may have been misguided or wrong, they said, but because Heffernan conceded that he was not exercising his First Amendment rights, he had no basis for his claim.