New answers tagged

0

Here's what worked for me to get the node type. In mytheme_preprocess_page(): if ($node = \Drupal::routeMatch()->getParameter('node')) { $variables['content_type'] = $node->bundle(); } Then I checked if content_type is == to what I want in page.html.twig


0

What the OP worked for me but I needed to override additional libraries: libraries-override: core/jquery.ui: css: theme: assets/vendor/jquery.ui/themes/base/theme.css: false core/drupal.dialog: css: theme: assets/vendor/jquery.ui/themes/base/theme.css: false core/jquery.ui.dialog: css: theme: assets/...


0

You can use the display name as part of the template name to confer specificity to your Views templates, e.g., [base template name]--[view machine name]--[view display id].html.twig, In your case, you could have names like views-list--products_section--product_shop.html.twig, views-list--products_section--computers.html.twig, views-list--products_section--...


0

The content type field is a reference field and contains the machine name of the content type as target id. Configure the field formatter Entity ID: Configure field: Content: Content type Formatter Entity ID ▼ Or use the property target_id when rewriting the field itself: {{ type__target_id }} You can add a second instance of the field and hide it if you ...


2

In a preprocess function, you can get that value with the following code: $use_default = \Drupal::config('theme_machine_name.settings')->get('logo.use_default'); You can then stick that Boolean in the $variables array and make use of it in a custom Twig template.


1

{% if title|render|striptags|trim %} should work. title|render to render, i.e. convert from render array to html string. |striptags to remove any html tags from the rendered title. |trim to remove any whitespace from beginning and end. If title is already an html string (i.e. not a render array) then you can omit |render and {% if title|striptags|trim %} ...


1

Okay, so It looks like this works: {% if title|trim|spaceless|striptags|length != 0 %} Which looks incredibly stupid but well...


0

Yes, but for some cases it is not clear, I find out that for every cases you can do something like that. You need to do something like that in your custom_module_preprocess /** * Implements hook_preprocess(). */ function MY_MODULE_preprocess(&$variables, $hook, &$info) { $route_name = \Drupal::routeMatch()->getRouteName(); if ($route_name == ...


5

addMessage() accepts a string or a MarkupInterface as its argument. If you intend to pass HTML to addMessage(), you need to construct a render array for that markup then explicitly render it. For example: $build = [ '#type' => 'container', '#markup' => t('Successfully submitted'), '#attributes' => ['class' => ['custom-messages--status']], ];...


0

The only way to pass values to a template file used for a theme function is adding them to the array passed as second argument of theme(). In the case of that chart_monitor theme function, theme('chart_monitor', array('classes' => array('new_chart_class'))); would add new_chart_class to the CSS classes. monitor_chart_preprocess_chart_monitor() doesn't ...


2

Yes, theme regions and CSS are what you need. Basically, from admin/structure/block you place your menu block into one of your theme's region. And then, with CSS you adjust the style of the region to your needs. If the HTML structure does not match your needs, you can also alter the page template of your theme (page.html.twig file). If this file does not ...


4

The 403 error page is not a redirect, so there is no referer. It's rendered in a sub-request which by default is using the route system.403. From inside of the sub-request you can get the route name of the original request with \Drupal::routeMatch()->getMasterRouteMatch()->getRouteName()


0

Create a tempalte webform-submission-form--{webform_machine_name}.html.twig Ex. webform-submission-form--contact-us.html.twig {{ form.elements.field_machine_name }} Ex. {{ form.elements.your_name }} {{ form.elements.email_address }} {{ form.elements.actions }} {{ form.form_build_id }} {{ form.form_token }} {{ form.form_id }} Make ...


0

I recommend the Twig Field Value module, which provides a field_value filter that you can apply directly to a field: {{ content.field_name|field_value }} Also, while this doesn't specifically answer your question, I would be remiss not to mention the handy and useful Twig Tweak module. It rocks! :)


0

I just had to do something similar; I added a lang attribute to rows in a search result. Here's what I ended up doing: Created an override template for the rows in my View of interest. In my case the name was something like "views-view-unformatted--my-view.tpl.php" I copied the existing base "views-view-unformatted.tpl.php" code into that,...


0

You can use something like this in your .theme file: function mytheme_page_attachments_alter(array &$page) { $apple_sizes = [57, 60, 72, 76, 114, 120, 144, 152, 180, 192]; foreach ($apple_sizes as $size) { $favicon = [ '#type' => 'html_tag', '#tag' => 'link', '#attributes' => [ '...


0

You can do something like this: in your new module my_prerenders in my_prerenders/src/ add a new class: namespace Drupal\my_prerenders; use Drupal\Core\Security\TrustedCallbackInterface; class MyPreRenders implements TrustedCallbackInterface { /** * @inheritDoc */ public static function trustedCallbacks() { return ['...


0

It's a simple way to add a class based on the URL, Drupal 9. without Modules. If you have specific requirement, then we can enable the modules. /** * Implements hook_preprocess_html(). */ function THEMENAME_preprocess_html(&$vars) { // Get the current path $current_path = \Drupal::service('path.current')->getPath(); $internal = \Drupal::...


0

You can first add theme suggestions for image elements based on the image style. Add this in your mytheme.theme file: /** * Implements hook_theme_suggestions_HOOK_alter(). * * Add theme suggestions for img elements. */ function mytheme_suggestions_image_alter(array &$suggestions, array $variables) { if (isset($variables['style_name']) && !...


1

item.content['#url'] gives a render array : However, if you need to extract only the URL which can be anything like frontpage, a node in your site or external, you need to use the .toString() utility. item.content['#url'].toString() Title can be accessed from item.content['#title'] Remember that it's all inside the loop: {% for item in items %} &...


0

Since the accepted answer is a link to a dead site I have copy and pasted the article here from the waybackmachine all credits to 'Nik' who submitted the article on 16 December 2015 - 3:04pm This took a bit more work to figure out than in Drupal 7! Bear in mind, this example is for installing a theme-wide (every page) font. There's references at the bottom ...


0

Adding this here which expands on the answer given here by @MiSHuTka (that answer work perfectly for most scenarios with cache data) Noting that both answers will not provide any extra query parameters in the destination query parameter. (if you wanted them to persist) Sometimes you need to add the html link to an render array item instead of the template ...


1

I would recommend to use https://www.drupal.org/project/twig_tweak module. <img src="{{ drupal_field('field_thumbnail_new', 'node', node.id, {type: 'image_url', settings: {image_style: 'YOUR_IMAGE_STYLENAME'}}) }}" />


0

You can try this: In your custom_module.module: function custom_module_theme() { return [ 'circulation_confirmation' => [ 'render element' => 'output', 'variables' => [ 'output' => [], ], ], ]; And in your controller: public function checkinConfirmation() { $rtn = [ ...


Top 50 recent answers are included