New answers tagged

0

None of the above solution worked in my case. Here how I solved it by coding function mytheme_preprocess_views_view_field(&$variables) { if ($variables['view']->id() == 'my_viewname' && $variables['field']->field == 'field_description') { $output = $variables['field']->getValue($variables['row']); $variables['output'] = ['#...


2

You can use: {{ item.content['#file'].filename.value }} Possible duplicate of: How do I get at the raw file name of a File entity in a Twig field override?


0

Figured it out. Had to use the striptags filter. This works like a charm! {% if php|striptags in field_dates|striptags %} True {% else %} False {% endif %}


0

I have a module by the name of amt_general and an entity type by the name of attendees and bound is attendance And bellow code load js just for edit and add the page of this entity and bundle. function amt_general_form_attendees_attendance_form_alter(&$form, $form_state) { // Unconditionally attach an asset to the page. $form['#attached']['...


0

Overide the menu.html.twig and Remove that line {% set link_attributes = link_attributes.addClass('dropdown-toggle').setAttribute('data-toggle', 'dropdown') %} with {% set link_attributes = link_attributes.addClass('dropdown-toggle').removeAttribute('data-toggle', 'dropdown') %} i.e setAttribute('data-toggle', 'dropdown') to removeAttribute('data-...


0

You can add the following statement to your Twig template to turn off caching. {{ {'#cache': {'max-age': 0}} }}


0

There are many ways to achieve this. Given you use a custom or sub-theme you'd follow Twig Template naming conventions and create your own field--node--MYFIELDNAME.html.twig template. And then simply adjust the markup according your needs. See Classy's field.html.twig for reference and its for-loop you simply can customize: {% for item in items %} <...


0

Your problem is coming from this code: $theme_registry['page']['path'] = drupal_get_path('module', 'example_ch_hero_panel') . '/templates'; The first array key, page, means you are working the page template. You've told the system to look for page.html.twig in your module's /templates directory. It's not finding it, so you get that error. Remove that hook ...


0

As you mentioned in the comments you field is multivalue field so you should loop over it and render the item inside <li> tag something like the following: <div class="lenders-block-content clearfix"> <ul> <?php foreach ($content['field_link'] as $key => $link):?> <?php if (is_numeric($key)): ?> <...


1

Yes. You can create a new library as follows: [THEME].libraries.yml mycontenttype: css: theme: path/to/my.file.css: {} js: path/to/my.file.js: {} Next, you attach that library to the node in hook_node_view_alter(): function THEME_node_view_alter(array &$build, Drupal\node\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\...


0

You can use path to theme (Uses the current allowed theme): <?php include(path_to_theme().'/templates/TEMPLATE_NAME.tpl.php'); ?> Or Drupal get the path (You can set the theme you want to use): <?php include(drupal_get_path('theme', 'THEME_NAME').'/templates/TEMPLATE_NAME.tpl.php'); ?>


0

To prevent caching of a particular block: add the below code in theme_name.theme file function theme_name_preprocess_block(&$variables) { if ($variables['plugin_id'] == 'block_id') { $variables['#cache']['max-age'] = 0; } }


2

You need a cache context and a cache tag of the user: $variables['username'] = [ '#markup' => \Drupal::currentUser()->getUsername(), '#cache' => [ 'contexts' => [ 'user', ], 'tags' => [ 'user:' . \Drupal::currentUser()->id(), ], ], ]; You can add this directly in a preprocess hook, but then the rendered ...


0

If you want to render a properly formatted single field of a referenced entity you'll simply have to use the |view filter on the desired field of the referenced node. Let's say the field_customer on my parent node references a customer content type entity that holds the customer's address in field_address. {{ node.field_customer.entity.field_address|view }}...


5

Although normally you render paragraphs recursively, you can retrieve a fixed structure non-recursively by recreating it with foreach loops: foreach ($node->field_paragraph->referencedEntities() as $paragraph) { foreach ($paragraph->field_media->referencedEntities() as $media) { ... } } This avoids accessing empty fields and is able to ...


5

Suggestion, create a domain object that has the logic. Normally these entities represent something that fits your business-domain. E.g in your instance, the node might be an event. So you might model a domain object called EventWrapper. <?php namespace Drupal\my_domain; use Drupal\node\NodeInterface; use Drupal\media\MediaInterface; use Drupal\file\...


0

Read you comments on my other answer. Here's how I would start tackling in your situation. I think you're going down a Form Alter method. Start off simple. Create a new module with just an .info and .module. In the .module, add code to find what you're looking for. KSM will show you the array and you can dig in. /** * Implements form_alter(). * form_id ...


0

After dealing with this for some time, I've found a potential alternative way of approaching this sort of functionality. For my use case, I was trying to add classes to the body of the node add/edit pages, so I can do things like hide blocks and alter positioning using css. My new approach to achieve this is to simply override the page.html.twig. So, if I ...


1

In some twig templates of the commerce modules, I can see the use of an alternate syntax: This is only in one module - Commerce Cart Flyout. These are Underscore.js templates. These are used to customize the JavaScript progressively decoupled cart flyout. These are not actually Twig templates. It was a design decision I regret to try and enable end-users ...


0

I think you need to check the file-suggestion by enabling in services.yml debugging, after that you will find the right Hook name. Then you can use.... function theme_name_preprocess_Hook_name(&$variables, $hook) { $variables['logopath'] = '/' . drupal_get_path('theme','theme_name') . '/logo.png'; }


1

To answer your questions: Yes, it executes on every page load. It won't impact your server load any noticeable amount. The amount of processing power for that one function is extremely low. You could probably run it 1000 times without causing a noticeable performance it.


0

$render_controller->view() invokes hook_ENTITY_TYPE_view() and this would be a better place to add custom values to the entity view. Although probably not good practice you could get your approach working by cloning the entity before modifying the field values: $entity = clone $entity; In any case you need to adjust caching so that you store for each ...


0

Every paragraph can be preprocessed with template_preprocess_paragraph or even more specific with template_preprocess_paragraph__PARAGRAPH_TYPE. Check this sample below, where I get the value of field_foobar of the paragraph type box and pass it to the Twig template. It's a little bit redundant as this value can be accessed from the template itself, but you ...


2

The vocabulary machine name is the bundle ID of the term, which you can get through the node object: {{ node.field_taxo.entity.bundle }} To load the label of the vocabulary: {{ node.field_taxo.entity.vid.entity.label }} This works only when the field has content. For an empty field you would have to dig into the field definition you get from the same ...


2

You can install Devel's submodule Kint, and then in your template print {{ kint() }} to get a pretty-printed dump of all available variables. Unfortunately I couldn't find the vocabulary label/name in there. Only the field label: {{ content.field_taxo_fruit['#title'] }} @4k4's answer below only prints the vocabulary's machine name. But not the label/name. ...


0

The posted responsive image configuration seems correct. Remember that you also need to change the image field formatter to "Responsive image". If you are using image fields on node entities, you can do this at /admin/structure/types/manage/MY_NODE_TYPE/display/MY_VIEW_MODE (If you are using image media entities instead of image fields, use the "Rendered ...


1

I don't know why it was not working because after I've done a simple <pre> {{ dump(myvariable) }} </pre> In order to check if the array was passed to the twig template, everything is working. So the solution is in my-template.html.twig {% for key, value in myvariable %} Key : {{ key }}<br> Date: {{ value.dateDebut }}<br&...


1

$ is a shortcode for jQuery, does your layout.js contain just $('.sidenav').sidenav(); Maybe try (function ($, Drupal) { $('.sidenav').sidenav(); })(jQuery, Drupal); You can read more here: https://www.drupal.org/docs/8/api/javascript-api/javascript-api-overview


1

User roles are defined in YML files called user.role.ROLENAME.yml. Do a config export (drush cex) to get an idea how these files should look like (they will be written to your config/sync directory). Then create your custom role YML files in my_module/config/install and they will be created when you enable your module (if you took a YML file from config/...


0

I had the same issue, but i was using {{ dump() }} to debug the template The problem I have the same arguments when i am using {{ dump() }} inside macro tags. For example: {% macro menu_links(items, attributes, menu_level ) %} {{ dump() }} ... code ... {% endmacro %} If you focus on function menu_links(), you realize that variables ...


0

ajax_example_autocheckboxes_callback function is not defined in the above code. Also please update more info about what you are trying to do. $form[$term->tid] = array(... 'ajax_example_autocheckboxes_callback' ...); //ajax callback $form['tree']; //new field. after the above function call you can get new built_info of $form_state as its rebuilt. So ...


0

I think the Form Options Attribute module will solve this for you. It allows you to add option attributes in the form element render array. Below is the example from the module page. It also supports checkboxes as well. $form['states'] = array( '#type' => 'select', '#title' => t('States'), '#options' => array( 'AL' => t('Alabama'), ...


0

Don't re-invent the wheel if these will solve your needs and hopefully wants. https://www.drupal.org/project/password_policy also worth looking into https://www.drupal.org/project/password_strength If those don't work, I think you're looking at a form alter related to validation. Drupal 8 is the wonderful world of Form Alters... so look into those even if ...


1

You can use the table theme type to do this. In your build section, return a render array that includes this: return [ '#type' => 'table', '#header' => $header, '#rows' => $rows, ]; Where header is an array of strings and rows is an array of row value arrays. Some reference items I found: New render element #type 'table' Render arrays


2

The easiest way would be to execute the View again in the page preprocess hook. If you want to avoid executing the View twice the task is getting a bit more tricky. This is because in D8 page elements are not necessarily rendered downstream in the order you expect. The element could have been cached in a previous request or in case the element doesn't ...


1

The pathing was wrong after all. The preceding "/" was messing things up. Strangely, this was not an issue for javascript attachments. For example: $form['#attached']['css']['css2'] = '/sites/all/libraries/data_search/tagme/tagme_jquery-ui.css'; //fails $form['#attached']['js']['js2'] = '/sites/all/libraries/data_search/tagme/tagme_jquery-ui.min.js'; //...


1

Views::getView is just a shortcut to loading the entity via the usual methods - it doesn't execute the View, so you won't see any results with the object you have. I'm not sure there's an elegant way to do what you need (although I hope I'm wrong, it seems like there would be). One simple-ish workaround would be to hook into the View's build process after ...


0

Here's a step-by-step: Install the UI Patterns Views module Insert the fields and map them to the UI Pattern fields Note: You are mapping fields for the rows. At this point, each piece of content should be rendered as a UI Pattern Now, if you want to arrange the cards in a specific way (say, using Masonry), then you can do it in one of the following ways:...


0

I had difficulties doing this too. The trick is to use getEntitiesToView() indeed. To get the function working, the following use statement is essential: use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase; Full code for the formatter can be found here: https://stefvanlooveren.me/blog/show-file-description-custom-formatter-drupal-8


0

Try adding the template array key to your theme hook: function footer_block_theme($existing, $type, $theme, $path){ return [ 'footer_menu' => [ 'variables' => [ 'address_title' => '', 'address_content' => '', ], 'template' => 'footer-menu', ], ]; } Or try adding the block twig template suggestion ...


0

In Drupal 8 use an Attribute object instead of a multidimensional array: D7 PHP: $attributes['attributes']['class'] = array(); $attributes['attributes']['class'][] = 'innerTab'; D8 Twig: {% set attributes = create_attribute() %} {% set attributes = attributes.addClass('innerTab') %} You can use the Attribute object in Twig link(): {{ link(title, ...


1

You can also get the active theme's logo path by using the theme manager service. \Drupal::theme()->getActiveTheme()->getLogo().


0

Normally you would iterate through the rows and call a row plugin to render the field values. As I understand the question you want to access the field values directly by row index and field id? Then try StylePluginBase::getFieldValue: $value = $this->getFieldValue($index, $field); Normally a style plugin iterates the rows like this: public function ...


0

You need to pass your file or image field uri value with file_url() function, Read the Drupal Docs, Then you will have field's exact value, not rendered {{ file_url(node.field_policy_pdf.entity.uri.value) }} - if it has limited (1) value. Else you need to use twig {% for %} loop


0

Works for me: /** * Implements hook_theme_suggestions_container_alter(). */ function THEMENAME_theme_suggestions_container_alter(array &$suggestions, array $element) { if (isset($element['element'][0]) && $element['element'][0]["#theme"]) { foreach ($element['element'][0]["#theme"] as $them_item) { $suggestions[] = '...


0

Thanks to what @Cesar Moore said and executing drush locale:check then drush locale:update and drush cr, my translation file is now working for my custom theme too :) FYI : my PO file is in a custom module folder. More refs : https://api.drupal.org/api/drupal/core%21modules%21locale%21locale.api.php/group/interface_translation_properties/8.6.x http://...


0

When you set rebuild in a form submit there is no controller involved. The next page displayed is the rebuilt form, where you can print the my_report twig template by adding it to the build array $form. You don't need to transfer the values in $form_state, because they are available while rebuilding the form. You can check in the form build function whether ...


0

I think using views for displaying a part of node fields on the node page is not the best way to do it. I would create a page template and a node template for Article content Type and display the needed fields there. You can find more about Drupal 8 theming here Also, training materials by Drupalize.me are fantastic. Let's do it the proper Drupal way.


0

It doesn't look like you've got properly formatted twig here - you're inserting Drupal tokens right into the template. That's not going to work, you're going to need to preprocess them somehow, and use proper twig. This template isn't any different from any others, so you can look at existing working templates for help. This will get you started on some best ...


1

Different Field information can be displayed in Views by using the Rewrite results and Replacement patterns options in the Field configuration. Different Field data is usually available as tokens (replacement patterns) that can be used to construct a different output than those available under the Formatter option. Here's an example for displaying the ...


Top 50 recent answers are included