WASHINGTON — President Trump wasn’t on the ballot or even stateside for Tuesday’s primary elections in Virginia and South Carolina. But he loomed over both states, just as he has in nearly every nominating contest this year, underscoring how the Republican Party has become the party of Trump and that its politicians cross him at their peril.

As Representative Mark Sanford of South Carolina found out the hard way, in his surprise primary defeat, having a conservative voting record is less important than demonstrating total loyalty to Mr. Trump, who now enjoys higher approval ratings in his own party than any modern president except George W. Bush following the attacks of Sept. 11. And in Virginia, a far-right candidate, Corey Stewart, won the Republican Senate nomination after waging an incendiary campaign and portraying himself as a disciple of Mr. Trump.

The president’s transformation of the G.O.P. — its policies, its tone, even the fate of its candidates — has never been so evident. A party that once championed free trade has now largely turned to protectionism under Mr. Trump. Sermons about inclusivity have been replaced with demagogic attacks on immigrants and black athletes. A trust-but-verify approach to foreign policy has given way to a seat-of-the-pants style in which rogue regimes like North Korea are elevated and democratic allies like Canada are belittled.

Mr. Trump’s harsh attacks, including describing the news media as “the country’s biggest enemy” Tuesday, draw muted responses or silence from most Republicans these days. The party’s lawmakers have seen what he can do to their campaigns, having witnessed how Senators Jeff Flake of Arizona and Bob Corker of Tennessee saw their standing with conservative voters plummet after they tangled with him. Neither is seeking re-election.