0

I need to run Database query and fetch the fields in my block named block--block--45.tpl.php.

How can I get subdomain table field values to my block tpl(block--block--45.tpl.php) file?

I tried this code in my block tpl file and its not working : $subdomain = db_query("select subdomain from {subdomain} where sid = :sid limit 0, 1", array(":sid" => $node->uid))->fetchField();

So anyone suggest an easy way to get subdomain table field values to my block tpl file.

  • Blocks are not really fieldable in D7 (unless you are using the Beans module). Also you are referencing a template file... I assume this is just to illustrate that you are interesting in the block with id=45, right? Can you be more specific about exactly what it is you are trying to achieve. I don't think your question is clear enough to be answered – bleen Jan 5 '17 at 17:58
  • Actually I am using Subdomain in my site. I have created the block for footer. I want to get subdomain field values from database. For that I have override the block as block--block--45.tpl.php and trying to get subdomain values from DB. But its not working. Is it possible to get DB values in block tpl file? – Mesk Jan 5 '17 at 18:04
  • Surely this is obtainable with views? Views will allow you to create a block, to place in any region, and accept a contextual argument to show fields from the current node. – Collins Jan 5 '17 at 18:31
  • I dint used Views block. I am using normal block. – Mesk Jan 6 '17 at 11:03
1

It sounds like you want to enable Tokens on your site and use a token to show the subdomain name in your block, after adjusting the Input Format for the block.

There really isn't coding involved, unless the subdomain module does not provide token support (then implement hook_token_info).

  • Let me know how can I achieve this by using Token? Send me the code or reference – Mesk Jan 6 '17 at 3:15
  • This doesnt require code. Simply enabling the Token module and configuring the Input Format to process tokens. Then insert the subdomain token as needed into your block text. A token looks like the string [some-example-token] – tenken Jan 7 '17 at 3:44
  • I have installed and configure token module. But I cannot able to find a token for Subdomain. – Mesk Jan 7 '17 at 3:46
  • The module may not provide such as token. Be sure to check under global or page tokens. you may find a subdomain or domain token ... I believe drupal has a before ".com" token by default (at least). If 1 is not provided you can always create your own token by creating a custom token in a module. Search this site for directions on creating a custom token if need be. – tenken Jan 7 '17 at 4:02
  • How can I get subdomain table field values to my block tpl(block--block--45.tpl.php) file? I tried this code in my block tpl file and its not working : $subdomain = db_query("select subdomain from {subdomain} where sid = :sid limit 0, 1", array(":sid" => $node->uid))->fetchField(); So please let me know easy way to get subdomain table field values to my block tpl file. – Mesk Jan 7 '17 at 4:11
0

You should NEVER run a database query from a template file. If you need some extra values from the database available to you your block you should create a process or preprocess function in your theme.

See https://www.drupal.org/docs/7/theming/overriding-themable-output/setting-up-variables-for-use-in-a-template-preprocess-and for more details.

  • How can I create process or preprocess for my specific block(block id is 45)? After creating this how I can call this to my specific block tpl file? – Mesk Jan 5 '17 at 18:11
  • Does this help: drupal.org/node/2275895 – bleen Jan 5 '17 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.