It's true that while Ted Cruz was serving as solicitor general of Texas, his office defended an existing law banning the sale of "obscene devices."
It's false that Ted Cruz banned (or attempted to ban) the sale of dildos in Texas, or personally supported a law that enforced such a ban.
Back in the 1970s, state of Texas banned the promotion of "obscene devices," which were defined to be items "designed or marketed as useful primarily for the stimulation of human genital organs," including "a dildo or artificial vagina."
In the 1985 criminal case Yorko v. State (brought by an appellant who pled not guilty to a misdemeanor information charging possession with intent to sell an obscene device, namely a dildo),
the Texas Court of Criminal Appeals ruled that the statute did not violate an individual's right to privacy, concluding that there was no constitutional right to "stimulate another's genitals with an object designed or marketed as useful primarily for that purpose."
In 2004, retailers wanted to sell such devices mounted a challenge to overturn the law, and in 2008 a three-judge panel of the U.S. District Court of Appeals' Fifth Circuit struck down the Texas law, concluding that it did indeed violate the 14th Amendment to privacy.
On 13 April 2016, a blogger for Patheos reported lawyer Ted Cruz, who was then serving as solicitor general of Texas (and is now a senator representing Texas in the U.S. Congress and a candidate for the Republican presidential nomination), had once argued a case that (in part) held that Americans have no inherent right to engage in masturbation:
Thus it should come as no surprise that as Texas solicitor general, a post he held from 2003 to 2008, Cruz defended ... a Texas ban on the sale of dildos and other sex toys, and in so doing made the absurd claim that Americans have no right to masturbate: "There is no substantive-due-process right to stimulate one's genitals for non-medical purposes unrelated to procreation or outside of an interpersonal relationship."
Patheos cited a 13 April Mother Jones article touching on the fact that Cruz had once "defend[ed] a law criminalizing the sale of dildos":
Yet one case[Cruz] does not mention is the time he helped defend a law criminalizing the sale of dildos.
The case was actually an important battle concerning privacy and free-speech rights. In 2004, companies that owned Austin stores selling sex toys and a retail distributor of such products challenged a Texas law outlawing the sale and promotion of supposedly obscene devices ... In 2007, Cruz's legal team, working on behalf of then-Attorney General Greg Abbott (who now is the governor), filed a 76-page brief calling on the US Court of Appeals for the 5th Circuit to uphold the lower court's decision and permit the law to stand. The filing noted, "The Texas Penal Code prohibits the advertisement and sale of dildos, artificial vaginas, and other obscene devices" but does not "forbid the private use of such devices." The plaintiffs had argued that this case was similar to Lawrence v. Texas, the landmark 2003 Supreme Court decision that struck down Texas' law against sodomy. But Cruz's office countered that Lawrence "focused on interpersonal relationships and the privacy of the home" and that the law being challenged did not block the "private use of obscene devices." Cruz's legal team asserted that "obscene devices do not implicate any liberty interest." And its brief added that "any alleged right associated with obscene devices" is not "deeply rooted in the Nation's history and traditions." In other words, Texans were free to use sex toys at home, but they did not have the right to buy them.
One of the more popular tidbits to emerge from that article pertained to the assertion that Cruz (via his legal team) had maintained:
In perhaps the most noticeable line of the brief, Cruz's office declared, "There is no substantive-due-process right to stimulate one's genitals for non-medical purposes unrelated to procreation or outside of an interpersonal relationship." That is, the pursuit of such happiness had no constitutional standing. And the brief argued there was no "right to promote dildos, vibrators, and other obscene devices." The plaintiffs, it noted, were "free to engage in unfettered noncommercial speech touting the uses of obscene devices," but not speech designed to generate the sale of these items."
It's important to note here that as solicitor general, Ted Cruz was in charge of the office tasked with representing the government's side of cases brought against the State, an office that was obligated to defend the law currently in place (i.e., argue in favor of upholding the state's ban on "obscene devices") as directed by the state's attorney general, Greg Abbott.
The case in question did not create a "ban on the sale of dildoes" (it was a challenge to an existing law to that effect), and the fact that the task of defending the law fell to the solicitor general's office (which was headed by Ted Cruz) does not mean that Ted Cruz personally approved of the law, or that he personally prepared or argued the case defending it. (As shown in court records, the State's side of the case was presented before the United States Court of Appeals by Bill L. Davis, not Ted Cruz.)
It is true that the controversial line quoted above (regarding there being no "right to stimulate one's genitals for non-medical purposes") appears in the Court of Appeals ruling as something referenced by the State (as corroborated here):
To determine the constitutional standard applicable to this claim, we must address what right is at stake. Plaintiffs claim that the right at stake is the individual's substantive due process right to engage in private intimate conduct free from government intrusion. The State proposes a different right for the Plaintiffs: "the right to stimulate one's genitals for non-medical purposes unrelated to procreation or outside of an interpersonal relationship."
However, as noted above, that statement flowed from a much earlier case, Yorko v. State (brought in 1985, long before Ted Cruz served as solicitor general), in which the Texas Court of Criminal Appeals had held that that there was no constitutional right to "stimulate another's genitals with an object designed or marketed as useful primarily for that purpose," as well another case brought in a different state, Williams v. Attorney General of Alabama, regarding a statute prohibiting the sale of "sex toys."
The most that one can make of this issue is to say that Ted Cruz once, in accordance with his job duties and the requirements of his office, oversaw lawyers who were obliged to argue in favor of an existing Texas state law that prohibited the sale of "obscene devices" (which included some sexual aids).