Firstly, I will say that Spanish LL "transforming" to Portuguese is not quite the right word. Rather we should say: if I want to guess a Portuguese word from a Spanish word, what should I write in place of "ll" in a Spanish word?
Now, the real answer is that to know when to do that, you will need to know Latin.
There are many Latin morphemes that we will have to analyse to discover the pattern.
- -culus
This suffix, seen typically as a diminitive suffix at the end of nouns or an instrumental suffix at the end of verbs, has a long history. Still in Roman times, the first 'u' was being dropped, giving us -clus. From this -clus, we have Portuguese -lho, Spanish -jo, Italian -cchio, and so on. So from Latin spĕcŭlu-, we have Portuguese espelho, Spanish espejo, Italian specchio. From oculu-, olho, ojo, occhio.
Exception: sometimes -culus will give Portuguese -cho (macho from masculus), but the same will be true in Spanish.
Exception²: the word mácula became mancha, malha (same meaning as mancha) and mágoa in Portuguese. Spanish only evolved mancha from this word. Both PT and SPN retained the word mácula through via erudita. Priberam claims that mancha comes from a vulgar *macella, but that does not make a lot of sense.
- -ll-
This morpheme in Latin words will become -ll- in Spanish and -l- in Portuguese. So we have sela and silla (from sella), castelo and castillo (from castellum),
Exception: maravilla does not become maravila, but maravilha, from Latin mirabilia. When we have this ending -lia in Latin, it often becomes -lha in Portuguese (exception is família).
- -li-
The morphem -li- will become -lh- in Portuguese but -j- in Spanish. Examples: alho and ajo (from alium), palha and paja (from *palia, vulgar of palea), borbulhar and burbujear (from bulbulliare).
- -cl-
This morpheme typically became -ch- in Portuguese and -ll- in Spanish. Examples: clamare became chamar and llamar, clave- became chave and llave.
Exception: not always. Claro conserved the cl- in both languages. Compounded and derived words also tend to conserve the -cl- (concluir from concludere and cluso was the ppp of choir from cludere).
- -pl-
Same as -cl-, it becomes -ch- in Portuguese and -ll- in Spanish. Examples: planus became chão and llano (but also plano in Portuguese and Spanish).
Exception: not always. Platea became praça in Portuguese instead of chaça, and placere became prazer for Portuguese and placer for Spanish.
- -fl-
It becomes -ch- in Portuguese and in Spanish -ll-. Examples: flamma becomes chama in Portuguese and llama in Spanish, plovere (vulgar of pluere) becomes chover and llover.
Exception: flos became flor in Portuguese and Spanish, fluxus became frouxo and chocho in Portuguese but flujo in Spanish.
- -tulus
This is a specific case, the Latin word vetulus meant old (velho, viejo). During the times of the Roman Principate/Empire the word came to be spoken as veculus (that much is attested), and from veculus we got velho, viejo, vecchio.
But of course, not every word in these languages comes from Latin. Some come from French (marchar), some from Gothic, some from English (cheque), some from German, and so on. Sometimes, Spanish will get a word from Portuguese, other times Portuguese from Spanish. In fact, there is no 100% certain way to make these cross-linguistic deductions without knowing the etymology of each word, which is much harder than just knowing each word.
When a modern word from Spanish with the graphem -ll- is loaned to Portuguese, the result is -lh-, and vice-versa. For loanwords from foreign languages, it will depend on what sound is there and how the natives of Portuguese and Spanish hear it.