Can they legally do that?
State Legislatures appoint Presidential electors as they see fit.
It so happens that all 50 have seen fit to use popular voting in some form to determine the choice of electors, but it is not required.
In fact, I don't think there is anything in the Constitution to govern or to restrict this power - a State Legislature could arguably say that no Catholics could be appointed as electors, for example.
So, a State Legislature is within its rights to say that Presidential electors will be appointed as a result of a "beauty contest" vote between the nominees of the Republican and the Democrat parties.
Yes, they can make it a condition of being included on the primary ballot. The parties have a lot of leeway in how they select their nominees. Now if Trump signed it and then decided to ignore it after the primary and run as an independent anyway, that statement wouldn't prevent him from doing so, but the "sore loser" law would.