New answers tagged

-1

Based on that structure, you're referencing the library incorrectly. It should be $form['#attached']['library'][] = 'bootstrap_subtheme/bootstrap_subtheme.global-styling'; As noted in a comment below, the duplicate use of the theme name may not be necessary, and $form['#attached']['library'][] = 'bootstrap_subtheme/global-styling'; should work according to ...


0

You can just add the full object to a twig variable, so $vars['myentity'] = $my_entity; and then access with {{ myentity.id }} A bit more elegant is to implement hook_theme: /** * Implements hook_theme(). */ function MODULENAME_theme() { return [ 'customentity' => [ 'render element' => 'elements', ], ]; } and a preprocess ...


0

The flag module has a utility function flag_get_flags() that retrieves multiple flags, with an option to filter on the entity type of the flag: // Same logic, looped over multiple node flags: $flags = flag_get_flags('node') foreach ($flags as $flag_name => $flag) { if ($flag && $flag->is_flagged($node->nid, $GLOBALS['user']->uid)) { ...


0

The form values still persist in the form state, and as they're not printed, the will be submitted as empty and saved accordingly. Do not hide form element by not outputting them in twig, render the full form there and use [#access] = FALSE; on the form elements you don't want to show. Do so in the controller, to only have that on your custom page. If you do ...


0

{{ simplenews_subscriber.field_lastname.value }} In the header of this template: Note that depending on the used caching strategy, the generated body might be used for multiple subscribers. If you created personalized newsletters and can't use tokens for that, make sure to disable caching or write a custom caching strategy implemention. To solve it I added ...


1

Add your own theme suggestion: mymodule.module /** * Implements hook_theme_suggestions_field_alter(). */ function mymodule_theme_suggestions_field_alter(array &$suggestions, array $variables) { $element = $variables['element']; // place the least specific suggestion at the beginning of the array array_unshift($suggestions, 'field__' . $element['...


1

If a module defines new twig filters or functions, they need to be manually excluded from the blt linter. Per a comment in the BLT issue queue, you can make these exclusions in blt.yml like: validate: twig: functions: - drupal_block - drupal_field - drupal_view - drupal_block - drupal_region - drupal_entity - ...


0

Turns out you have to provide a CSS path for your less files using the css_path key that points to where to find the less's compiles css: less/landing.less: { css_path: "/css/" }


0

I got the media description in the preprocess function template_preprocess_media(array &$variables) { $bundle = $media->Bundle(); if($bundle == 'file') { $field_media_file = $variables['content']["field_media_file"]; if($field_media_file[0]["#description"]){ $desc = $field_media_file[0]["#description"...


1

I think you should add your module's CSS via a m̀y_module.libraries.yml instead. And just for the sake of completeness: You could also implement hook_library_info_alter and move your module's library after your theme's library. The already suggested solution using weight is preferable though. If you need conditional logic for adding the module's CSS, the ...


1

You can define the priority for the css to load , Add weight key for the CSS file in the *.libraries.yml , the lesser the weight the higher the priority. cuddly-slider: version: 1.x css: theme: css/cuddly-slider.css: {weight: -50}


0

The following answer is credited to @leymannx. {% if not remove_searchbar %} do something {% endif %} After some research I found twig syntax. You also can find the "if statement" syntax and samples on Twig Symfony website.


0

This is an example code of macro. {% macro menu_links(items, attributes, menu_level) %} {% if items %} {% if menu_level == 0 %} <ul id="menu" class="menu"> {% else %} <ul class="menu sub-nav"> {% endif %} {% for item in items %} {% set classes = [ 'menu-item', ...


1

The screenshot is a render element of a link which gets printed in the template completely. What you find inside is for internal usage and is most times protected anyway. To get this request parameter in your template build your own variable in preprocess: $variables['path_info'] = \Drupal::request()->getPathInfo(); You might also need to add a cache ...


3

You can put a template_preprocess() function in a custom module or your theme mytheme.theme file. It depends on what you want to achieve. If this preprocess function is specific to the theme (for example, the results are used in a theme template), then you should put it in mytheme.theme. But, if you put the code in mytheme.theme and then change themes, your ...


1

Another way to compile sass is to use gulp. There's a slightly dated general purpose tutorial here. And there's a git gist for the Drulma theme I wrote here. What gulp allows you to do is to chain various processes together, so you can, for example: Compile the sass with sass Minify the sass with cssnano If you're using a framework like Bootstrap or Bulma, ...


0

There's no need to edit the CSS directly, as the module already allows for styling. To style the Gutenberg editor, follow these instructions: You can "inject" stylesheets and scripts into the editor using the libraries-edit at [your_theme].gutenberg.yml configuration file. Example: my-theme.gutenberg.yml: libraries-edit: - my-theme/editor my-...


0

I would highly recommend using the Twig Tweak module to get the URL from an image field in your twig template. Once you have installed the module you can then use various methods for extracting the URL from the image field. The following worked for me: {{ node.field_main_image.0 | file_url }} I set the background image of a <div> with the following: &...


2

Yes. Sometimes themes will drop documentation when they override a template, but you can always check the original template file for the available variables and other info relevant for working with the template. If you check the node.html.twig template provided by the node module in core, you'll see the following documentation at the top of the file. /** * @...


0

I resolved my issue, using solution below {% set Paragraphs = node.body.value|split('</p>') %} {% set paragraphHalf = (Paragraphs|length / 2)|round %} {% if paragraphHalf %} {% for paragraph in Paragraphs %} {% if paragraphHalf == 1 or loop.index % paragraphHalf == 1 -%} {% endif %} {{ paragraph|raw }} {%...


2

This is not working, you can't use a form template in a controller. You have to assign the template to $form['#theme'] when building the form or in a form alter hook. Then it gets used by formBuilder()->getForm() automatically. See for example seven_form_node_form_alter(): $form['#theme'] = ['node_edit_form']; and the corresponding twig file for a two ...


0

I have never herd of "breakpoints.yml" and don't think you can do what you want like that so i think you just need to make normal media queries in your css file "css/style.css" if you want you can break them out in to other files but really there is no need. Example: // Extra small devices (portrait phones, less than 576px) @media (max-...


2

Why does the login form become unusable Because input.html.twig is "Default theme implementation for an 'input' #type form element". If it's empty, there will be empty string in place of all <input> tags. If you have cleared content form input.html.twig placed in your custom theme, just completely remove input.html.twig file and clear the ...


0

you can get the summary with summary attribute in field level. {{ content.YOUR_FIELD_NAME['#items'][0].summary }} in your case answer will be : {{ content.field_description['#items'][0].summary }}


1

None of the suggestions so far worked for my text field. My text field is not multivalue, just a simple text field. Here is what did work for me: content.field_myfieldname['#items'].getString()


0

With twig tweaks you don't need the twig_field_value module as in @paulcap1's answer. {% set image_uri = content.field_sbb_background_image['#items']|file_uri %} {% set image_url = file_url(image_uri) %} Or if you are printing out with an image style you don't even need the image url. To print, say, the image style thumbnail: {{ image_uri|image_style('...


1

Having tried to do this myself before, this is probably not a route you want to go down. This approach becomes harder to maintain as you install more modules (different modules can inject CSS on different paths), and often can turn into a maintenance nightmare. Drupal can aggregate CSS files out of the box; this is often all you need to improve performance ...


1

This is how block templates work, they render the variable content. {{ content }} Your code though is using a block template inside of a block, which doesn't make sense, because the block template is wrapped around the block by Drupal automatically. You probably want to build the inside of the block like a controller output by using a custom template not ...


1

You don't need this code by the way, it is in core since D8.8.x, see https://www.drupal.org/node/2960810. If you want to change the status code then do this in an event subscriber. If you want Drupal to build a normal 404 page and only change the status code of the already built error page then use a response event subscriber and set the status code: $...


-2

you can use hook bellow (check for the right path) function MYMODULE_page_attachments_alter(array &$attachments) { if(\Drupal::currentUser()->isAnonymous()) { unset($attachments['#attached']['library']['core/modules/system/css/components/hidden.module.cs']); } }


Top 50 recent answers are included