Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

New answers tagged

1

You can't set a value for "nothing" aka "Global: Custom text" in hook_views_pre_render() because _entity is the entity object. The entity (Taxonomy Term in your case, and Node in my test case) does not contain this field. Therefore, you need to use template_preprocess_views_view_field function THENAME_preprocess_views_view_field(&$vars) { if(isset($...


1

You clarified in the question's comments that you want to change the node title just on the default node display (i.e. Full content). You can change the title by implementing template_preprocess_page_title(&$variables) in your theme's THEME_NAME.theme file, and changing $variables['title']. In the following code, only the product node type will have its ...


0

Would you want to use Automatic Entity Label to actually change the title?


1

When using {{ content.field_author_name.0 }} it works because field is rendered with the twig. The ~ operator converts all operands into strings and concatenates them. Given that content.field_author_name.0 is a render array you need point to the string value of this field. To do this you can inspect it with the devel or twig_vardumper module using dump(...


0

To get the author name you can use {{ node.Owner.name.value }} or {{ node.uid.entity.name.0.value }}. Try with: {{ node.changed.value|date('m.d.y') ~ ' | By, ' ~ node.Owner.name.value }}


0

From my experience, there's no easy way to twig in views-views-unformatted--view-.html.twig a value from fields not part of the entity structure. Very to print a text (plain) field. So painfull to print a flag_field, entity_reference and so on. Having to achieve this without going through the whole view core code, I had to build this hack: add a fake ...


0

@NikLP I got "This block is broken or missing. etc" until I used the value of the #plugin key in the Devel output for my Custom Block. This was the output in Devel: Block {#725 ▼ #id: "mycustomblock" #settings: array:7 [▶] #region: "content" #weight: 0 #plugin: "block_content:de79f8de-8e8f-4a56-b787-790347b4f104" ... Then in my template, this ...


5

The answers so far are only valid for non reference fields. A reference field is different, because the referenced entities can have different access results than the parent entity. The rendered field can be empty although the field in the node object is not. As explained in How do I hide an empty field if my custom field formatter removes all items? there ...


0

Twig blocks are named control structures, you can't define them multiple times within the same scope. Either try to move the if statement inside one single block {% if topic_type %} {% embed "node--teaser__field.html.twig" %} {%- block field_icon -%} {% if topic_type == 'test' %} icon 1 {% else %} icon 2 {% ...


0

I believe you are using the module Field Formatter Class to have this option for your fields. The module does not set a maximum value for this field. So I believe it is using a default of 128 chars in core. 128 is a fairly long limit for a class field. But if you want to alter this you can write an alter function in your module like they have done in ...


0

My own bullet-proof version that worked even in the most weirdest edge cases: it works for all field types it checks if the field exists it checks if the field is not empty it checks if the field is not hidden in display mode {% if not (node.field_whatever.isEmpty == true) and content.field_whatever is defined %} <div class="whatever_markup"> {{...


0

Is content.field_name|render|striptags|trim still the only way? That's a way a lot of people choose to do this check, if you're doing it purely in twig. But I would argue it's not the only way to achieve this. What you can also do is check the node (or paragraph, term etc any Drupal fieldable content entity) object's field instead of the content array. So ...


0

You could do this with a preprocess function. The code here works for full views only (it won't work e.g. in a view of multiple teaser nodes). function THEME_preprocess_html(array &$variables) { $router = \Drupal::routeMatch(); $node = $router->getParameter('node'); if (is_numeric($node)) { $node = \Drupal\node\Entity\Node::load($node); } ...


0

To add dynamic libraries as shared libraries, check the following hook_library_info_build() for dynamic library definitions You can add javascript libraries to a theme like the following Adding Javascript to a D8 theme You can add javascript libraries to a module like the following Adding Javascript to a D8 module


2

If this is in Drupal 8, you can export all config and search for the display id in both code and config and you should be able to find if it is being used anywhere.


0

You can add a custom page template for your search results page and then print the pager above the {{content}} You can use the template name suggestions as listed here Twig template naming conventions - Drupal 8 You can also use a preprocess function to duplicate the pager in the render array Preprocessing output in Drupal 8 - Acquia


0

I know this question refers specifically to formatting the value of a given date field, but I needed to format the date the node was created. In case you need that, this is what works for me (after trying several different options): {{ node.getCreatedTime|format_date('medium') }} This is where I got this solution from: https://www.drupal.org/forum/support/...


1

Create a theme hook with the template name by replacing dashes with underscores: mymodule.module: /** * Implements hook_theme(). */ function mymodule_theme() { return [ 'block__bundle__jumbotron' => [ 'render element' => 'elements', 'base hook' => 'block', ], ]; }


1

Not being able to find better solution nor documetation, I built this working hack: add a fake text field entity. IE: for articles: http://yoursite/admin/structure/types/manage/article/fields > Add field > Add text plain field hook the view in a custom module: function MYMODULE_views_pre_render(ViewExecutable &$view){ if( $view->id() == 'VIEWID' )...


0

It seems that something has changed and variables['elements']['#block'] is not available anymore. Here is what worked function ThemeName_preprocess_block(&$variables) { $block = entity_load('block', $variables['elements']['#id']); $region = $block->getRegion(); if ($region == 'nav') { $variables['attributes']['class'][] = 'SomeStyle'; } ...


3

You can get the alt of image with: item.content['#item'].value.alt


1

Use hook_theme() inside your theme's template.php file to set a custom template file (tpl.php) for the taxonomy add and edit forms. However, your theme needs to be set as the Administration Theme, even if Use the administration theme when editing or creating content is not checked. The taxonomy module implements only one form template file for the taxonomy ...


2

By concept a contrib module will hardly ever be able to use SASS, because SASS simply is not part of Drupals distribution. But that doesn't mean your local environment cannot use shared SASS resources for custom themes and custom modules. E.g. in all our projects, the project-specific theme is a sub-theme of a shared, immutable base theme providing defaults ...


3

Several things are wrong here. Your hook_theme must return: return [ 'return_products' => [ 'variables' => [ 'products' => NULL, ] ] ]; Now you have a template called return_products with a variable called products. In your controller you must then use '#theme' => 'return_products', instead of '#theme' => 'return-products'...


3

You have to add the testmenu variable to the hook_theme implementation, like this: function mdoner_custom_theme($existing, $type, $theme, $path) { return [ 'mdoner_custom_block' => [ 'variables' => [ 'title' => NULL, 'testmenu' => [], ], ], ]; } Then you have to pass it to the render array in your build() ...


0

So the node in question had a field field_comments, from which I was trying to access the comments. I found a different way of doing that - I created a MYMODULE_preprocess_template function, loaded all comments of the node in that function, created a variable "comments", where I stored the comment data I needed and passed it on to the twig template. Here's ...


0

Use ConfigEntityBase::status: if (isset($lang_block) && $lang_block->status()) { // block is enabled }


0

I think you should send render of the block not an array of block entity. View Method return an array. Try with renderer service like the following: /** * Implements hook__preprocess_HOOK(). */ function mymodule_preprocess_node(&$variables) { $node = $variables['elements']['#node']; if ($node->bundle() == 'page') { $block_entity = Drupal\...


0

I suppose the elements documentations is a bit misleading. You can always try debugging like this: {{ context|keys|join('+') }} {{ data|keys|join('+') }} {{ data|join('+') }} which brought me to conditions like {% if data.mycheckboxes %} {% if mykey in data.mycheckboxes %}


0

You have to set "node" in the twig-variables of your block. I.e. you can do this in a template_preprocess hook like so: function mytheme_preprocess_myBlock(&$variables) { $variables['node'] = Node:load(50); } Then you can access the "node" in your template like {{node.my_field.value}}. The hard part is to find which node to load in the preprocess ...


1

What is the name of your template file? Depending on what template you use, {% if view.total_rows > 6 %} <div ... {% endif %} should work.


1

You must add a file to your template called field--comment.html.twig Use the following code. {# /** * @file * Theme override for comment fields. * * Available variables: * - attributes: HTML attributes for the containing element. * - label_hidden: Whether to show the field label or not. * - title_attributes: HTML attributes for the title. * - ...


0

I recommend reading this page. Install the twig tweak module. And use the link described above.


3

You'll have to manually type it on your twig, as per https://www.drupal.org/project/twig_tweak/issues/2738781 If you don't want to type it on twig, a work around would be to add a "Text area" to your view's Header and use replacement patterns, <div class="view-title">[view:title]</div>. Alternatively, you could ditch Twig Tweak and do it ...


1

First , Check Administrator -> Configuration -> Media -> File System to personalize your tmp path. Make sure to use relative paths and have the right privileges for your webserver. After than Check permission of the /sites/default/files and make sure it's writeable by your web server.


2

item.content works only in the items loop, but don't try to put rendered output in a class name. Use the field value instead, which you can get through the field object #items stored in a field template inside of the variable element: {{ element['#items'].value }} or from a different field of the parent entity: {{ element['#object'].field_example.value }} ...


0

In twig, we can get the path - <img src="{{ node.FIELD-IMAGE-NAME.entity.uri.value | image_style('news_image_full_width') }}" alt="">


0

This is working code on how to do this in Twig without contrib. You can change the '#style_name' parameter to play with making this dynamic for a multifield. {% for item in content.field_image['#items'] %} {% set image = { '#theme': 'image_style', '#style_name': 'medium', '#uri': item.entity.uri.value, '#alt': ...


1

It looks like you are messing around with your naming. Since your module is called test_twig_template you must use that name for hooks and namespaces etc. Fix these things and it should work. Rename your hook_theme() from test_twig_theme to test_twig_template_theme. Call your route test_twig_template.theme. Your controller definition in your ...


0

If you want to remove twig debug comments you can use striptags filter Like the following: {{ fields.view_node.content|striptags}}


0

That line isn't loading any Javascript file from an external URL. It simply defines the document type as XHTML+RDFa (an extended version of the XHTML markup language that supports RDF), the XML namespaces used in the document, and a CSS class used for the <html> tag. Actually, nothing is loaded from those URLs, which are simply used as namespace names. ...


2

The function would be get_browser. But you should be using Javascript to detect this instead of PHP. There are a variety of ways it would fail/be incorrect from the PHP layer, caching, spoofing, etc. Once it gets cached, it would show up for the next person regardless of what browser they are using. Plenty of JS libs out there can detect it as close as ...


0

I solved this using below CSS .field--name-field-tags .field--item { display: inline; } .field--name-field-tags .field--item a, .field--name-field-tags .field--item a:visited, .field--name-field-tags .field--item a:active, .field--name-field-tags .field--item a:hover { color: #fff; background-color: #ff5588; padding: 1px 5px 1px 5px; border-...


3

As 4k4 said in the comments, the route is being generated using the link template in the entity annotations. Changing these link templates should be how you set the path to any custom entity page. For future content entities, drupal console will use the base path specified in this step: Enter the base-path for the content entity routes [/admin/structure]


1

Use the Browser detect module , it will add browser class to html tag as well. This project allows you to identify the browser and the device used. A class is inserted in the HTML tag so you can use as you need. Also, you can detect it by calling this function after install this module Drupal.behaviors.browser_detect.browserVersion();


0

Best approach for this is to use print step by step. Use echo ''; var_dump($variables); This will fetch the result in readable format.


3

Since the dot syntax can mean a lot of different things, try to make it more clear by using the array syntax: {% set the_variable = 'mytheme_myblock' %} {% if page.header_first[the_variable] %}


0

/* * hook_form_views_exposed_form_alter */ function THEMENAME_form_views_exposed_form_alter(&$form, &$form_state, $form_id) { if($form['#id'] == 'views-exposed-form-global-search-default'){ $form['#attributes']['class'][] = 'row popup-form'; //add form class $form['combine']['#attributes']['class'][] = 'search_input'; //add class to ...


1

you were very close ! try twigs "in" . for explanation see https://stackoverflow.com/questions/8319011/if-a-true-or-b-true-statement // remove the first line obviously. {% set blockmachinename = 'hello' %} {{ blockmachinename }} {% set full_block_name = 'page.header_first.' ~ blockmachinename %} {{ full_block_name }} {% if full_block_name in ['page....


1

{% set extension = node.field_event.entity.filemime.value|split('/') %} {% set extensionDot = '.' ~ extension[1] %} // File name {{ node.field_event.entity.filename.value|replace({extensionDot : ''}) }} // File size {{ node.field_event.entity.filesize.value / 1000|round }}KB // File extension {{ extension[1] }}


Top 50 recent answers are included