Score:0

How to put a phone number in one place and call it from various other places?

sg flag

Let's say I want to store my phone number in some "Drupal variable" and then call it anytime I need it, in, say, a node or a block.

The purpose is not the repeat the phone number and change it from one place in case I replace my number to a new number.

How to do that?

I would assume that I would have to install the module Token for that but is it the way or is there another way?

in flag
Normally, you'd create a Contact content type with telephone number and other contact details. Then on other entities like nodes, blocks, etc. you create an entity reference field to nodes of this content type. Doing it this way will also allow you to store other contacts as well in the event that you need to store more than one, future-proofing your approach.
sg flag
@Joseph I still need to put the number only in one place and then call it with some syntax that will transform into it, pretty much like "variable substitution" in Bash.
Score:0
cn flag

If you want something like variable substitution, yes you can use Token module for that. You will need to create a custom module and define your own tokens. This is because the Token module only makes tokens available; it does not provide a UI for defining them on its own.

Also, normally, tokens cannot be used in input filters, which means tokens won't be available in node bodies, among other places. To get tokens in node bodies, you also need the Token Filter module.

I would prefer the Token module in this case because the base token functionality is part of Drupal core and enjoys full support throughout the Drupal ecosystem.

sg flag
I think I could also write something like `$phone` and then let a global JS file declared in info.yml to change it in each page load, although that may cause some kind of "Flash of unstyled content".
cn flag
@phoner You could do that but then your site will be dependent on JS for text and search engines may not reliably pick up the replacements. Custom tokens take some time to set up, but if you want a reliable solution into the future, that's the way to go.
sg flag
Oh, sure, I agree, but in this very particular case of phone number I actually don't want search engine indexing ^__^
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.