Let's say I create a function in a module file:  function _get_test() {   return 'test'; }   How can I call this in any Twig file? For example *.html.twig, page.html.twig, node.html.twig etc.   OR   How can I pass a variable from PHP to Twig and display in any Twig file.  For example *.html.twig, page.html.twig, node.html.twig etc.
121595
I work in D8, I create a custom module first_module. By modules create a url and display all nodes by *.html.twig file.  Write code in controller.php  public function contentthree() {    $connection = Database::getConnection();    $query = $connection-&gt;select('node', 'n');   $query-&gt;fields('n', ['nid', 'type']);    $groups = $query-&gt;execute()-&gt;fetchAll();    foreach ($groups as $group) {     $events[] = [$group-&gt;nid, $group-&gt;type];   }    return [     '#theme'  =&gt; 'first_module_display',     '#events' =&gt; $events,   ]; }   In *.module  function first_module_theme($existing, $type, $theme, $path) {    return [     'first_module_display' =&gt; [       'variables' =&gt; ['events' =&gt; NULL],       'template'  =&gt; 'first-module-list',     ],   ]; }  function _get_node_details() {    $output = 'Welcome D8';    $output .= 'Node Details';    return $output; }   and create templates first-module-list.html.twig and display data  {%for key, data in events %}  {%for key1, data1 in data %}     {{ data1 }}  {% endfor %}  {% endfor %}   work fine and showing data correct. But, my question is how to call function _get_node_details() and how to render node details in first-module-list.html.twig files?
91117
I'm looking for a good approach to manipulate some JS code from within a module call. (Using Drupal 6.)  I have a set of JavaScript functions I need to add to a page. (Regular JavaScript, no jQuery.) Unfortunately they rely on some variables from the page they are shown on. So my current approach is to generate all the JavaScript from within PHP code which results in a whole lot of escaped strings like $output .= '  var url = "'. $url . '";' . "\n";, which is not so nice after 20-30 lines of code.  So I would like to move all the JavaScript code into it's own .js file, and find a way to inject my variables. So far, the only solution I could come up with is putting it in a temporary file, read that, modify and save it to the /files directory. But is there another, more preferred way?
53525
How to print fields value in views-view.html.twig I have created a views-view.html.twig file for my view slider_3. The template name is 'views-view--slider_3.html.twig' The view formatting is done by this template, but I need a field is come at begging position of the view:  &lt;div class="slider-3-main"&gt;{{ rows.required_field }} &lt;div{{ attributes.addClass(classes) }}&gt;   {{ title_prefix }}   {% if title %}     {{ title }}   {% endif %}   {{ title_suffix }}    How can be the field will written to display at {{ rows.required_field }}
78348
You can do this in Python, but is it possible in PHP?  &gt;&gt;&gt; def a(): print 1 ...  &gt;&gt;&gt; def a(): print 2 ...  &gt;&gt;&gt; a() 2   e.g.:  &lt;? function var_dump() {} ?&gt; Fatal error: Cannot redeclare var_dump() in /tmp/- on line 1
91306
I hope someone is able to help me. The problem is the following one: 1)I have module in which a assign the variable in order to send to the template file php.tpl  &lt;?php ... $testvar="Hello from alex!"; $variables['testvar'] = $testvar;  ... ?&gt;   This variable can be shown in the php.tpl file like   &lt;?php print $testvar?&gt;   2)I have separated .js file How can i get access to this varuiable inside .js file?  I know how it'll be if .js file is inside .php.tpl:  &lt;?php $testvar="Hello from alex!"; ?&gt; &lt;script type="text/javascript"&gt; var myVar = '&lt;?php print $testvar?&gt;'; &lt;/script&gt;   The problem is how to do the same if these two files .js and .php.tpl are separated?
54685
In the module I am developing I need to add script which require jQuery.The will be added depending on configuration settings. Hence I am using template_preprocess_module(&amp;$variables) where I am trying to add script by doing something like this:  function template_preprocess_module(&amp;$variables) {   if (TRUE) {     $variables['jquery'] = ?;     $variables['script'] = 'path to script in module';   } }   Then in the template I will simply be rendering these variable in the head section. But I am facing trouble in adding jQuery, because I don't know its source (obviously I know its source in core but I need something over here which automates path). Can the source be known by using something like drupal_get_path() ? I am also not sure if this is the best way to add script conditionally, any suggestion regarding this? Thanks.
89533
I need Drupal to use a custom page template if the URL contains a particular _GET parameter.  In D7 I would do something like this in the theme's template.php:  function mytheme_preprocess_page(&amp;$variables) {       if($_GET['isCustomTpl'] == true)         $variables['theme_hook_suggestions'][] =  'page__myCustomTpl'; }   It doesn't work in D8 though (I would have been surprised by the opposite, considering all the architectural changes in the theme layer).  Any ideas?  Bonus question: is it possible to define such a function in a module, so that i can release it publicly without having the users to edit their themes?
120697
I'd like to provide template implementation in my module and allow themes to override it. Basically, I add a suggestion by this simplified code:  function attach_preprocess_node(&amp;$vars) {   $vars['theme_hook_suggestions'][] = 'node__test'; }   (I don't want to use hook_theme to add a new theme because I want to reuse preprocess node functions. The theme name are awkward but I don't want to write node_attach_% to avoid confusion with node type.)  Then I use hook_theme_registry_alter() to add module path:  function attach_theme_registry_alter(&amp;$theme_registry) {   $path = drupal_get_path('module', 'attach') . '/themes';   $theme_registry_copy = $theme_registry;   _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'node', drupal_get_path('module', 'node'));   $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);   if (!isset($theme_registry['node']['theme paths'])) {     $theme_registry['node']['theme paths'] = array();   }   if (!isset($theme_registry['node']['theme paths'])) {     $first_element = array_shift($theme_registry['node']['theme paths']);     if ($first_element) {       array_unshift($theme_registry['node']['theme paths'], $first_element, $path);     }     else {       array_unshift($theme_registry['node']['theme paths'], $path);     }   } }   However it does not work. That means: the file themes/node--super.tpl.php is not used. It is used only if I copied it to the theme folder.
2668
In my module I have some custom strings. I use these in the t() function. When I go to admin/config/regional/translate/translate and filter the untranslated strings my custom strings do not show here.  What could be going wrong?
58594
I'm creating custom templates for my Drupal 7 site. There is a custom content type called flowers.   Which template file (.tpl.php) should I put under templates directory of my theme's folder for displaying a single flower-type node? Can I use the Views module to create different views (i.e displaying picture of a flower in a view &amp; displaying species of the flower in another view) for displaying only a single flower-type node? So far I can create pages that displays list of flower-type nodes. I want to display just a single (perhaps providing it's node-id) flower-type node. How do I do that?  2.1. For displaying different types of views to display a single flower-type node, which template files (.tpl.php) should I edit?   Thanks in advance for caring :)
4613
How can I create or use a global variable in a function?  If I create a global variable in one function, how can I use that global variable in another function? Do I need to store the global variable in a local variable of the function which needs its access?
123057
I am using hook_theme_suggestions_HOOK() to implement a custom template file in my drupal 8 website like below.   /**  * Implements hook_theme_suggestions_HOOK().  */ function mymodule_theme_suggestions_page(array $variables) {   $template_suggestions = array();   // This is used to display only $content.   if (isset($_GET['ajax']) &amp;&amp; $_GET['ajax'] == 1) {     $template_suggestions[] = 'page__ajax';   }   return $template_suggestions; }   Above code is working fine and I can add template page--ajax.html.twig inside my current active theme and changes in template is reflecting in page.  But when I put that file inside my module itself file is not detecting and changes not reflecting in page and taking default page.html.twg template file.  How can I provide default template file within my module itself?
315
I'm working on a module in Drupal 7. I've added a hook_menu implementation:  $items['admin/config/content/mymodule'] = [   'title'            =&gt; 'MyModule',   'description'      =&gt; 'Configure MyModule settings.',   'page callback'    =&gt; 'mymodule_get_form',   'page arguments'   =&gt; ['mymodule_admin_settings'],   'file'             =&gt; 'mymodule.admin.inc',   'access arguments' =&gt; ['administer mymodule'],   'menu_name'        =&gt; 'mymodule', ];   ...and a configure line to mymodule.info:  configure = admin/config/content/mymodule   A Configure link for MyModule now appears the admin/modules page, but how can I also have the module listed on the admin/config page? Or is the admin/config page reserved for only core modules?
47148
I'm trying to create a block that will pass a variable to its template. For now just some text but it will be an array later.  The default block variables such as label are working but I'm unable to get the value to show up in template.  Any ideas ?  FrontAppsBlock.php  namespace Drupal\custom_blocks\Plugin\Block;  use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockPluginInterface; /**  * @Block(  *   id = "front_apps_block",  *   admin_label = @Translation("Front Apps Block"),  * )  */ class FrontAppsBlock extends BlockBase implements BlockPluginInterface {    public function build() {     return array(       '#theme' =&gt; 'front_apps_block',       '#app' =&gt; 'test value',     );   }  }   custom_blocks.module  function custom_blocks_theme($existing, $type, $theme, $path) {   return [     'front_apps_block' =&gt; [       'variables' =&gt; [         'app' =&gt; null       ]     ]   ]; }   block--front-apps-block.html.twig  &lt;p&gt;test: {{ app }}&lt;/p&gt; &lt;p&gt;label: {{ label }}&lt;/p&gt;
65942
I need to render a views block in my page.html.twig template. In D7 I would do this:  &lt;?php   $block = module_invoke('module_name', 'block_view', 'block_delta');   print render($block['content']); ?&gt;   In Drupal 8 module_invoke is deprecated and recommended to use this: (I've added the block's name as the second parameter)  Drupal::moduleHandler()-&gt;invoke($block, 'views_block__blog_block_1', $args = array());   I tried a few things. First tried to do it in a twig template but I don't know how to call php functions in a twig template, so that didn't turn out too well.  Then I called the function in the preprocess_page() function in the .theme file but before I could get it to work I tried something simpler just to try make a variable work inside the twig template which didn't work either, for example:  In the template_preprocess_page(&amp;$vars) function in the .theme file:  $test = 'Hello World'; $vars['$my_var'] = $test;   I tried to call my_var inside the twig template but it didn't work and I got an error message saying "The site has an error, please contact the administrator"  So, to summarize, here are my questions:   How do I make variables available inside twig templates? How do I call functions inside twig templates? Do I render blocks inside the .theme file or twig template?
69441
I would like to create a function that operates on a table, for example  create or replace function test(t table)      returns void language plpgsql as $func$ begin        select * from t limit 10; end; $func$   Then, I can call the function with any tablename, e.g.  select test(myTable);   How would I do something like this?
42226
Basically it was  but I'll reword it, as I don't want to define a new theme (reason was already given).  What I want to do: in my module I have a file node--attach.tpl.php which is a modifed version of node.tpl.php. Now I want that, if view_mode = 'attach' then this file is used. Also, theme should be allowed to override it with its own node--attach.tpl.php in theme folder.