<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TrainingCloud.io</title>
    <description>The latest articles on DEV Community by TrainingCloud.io (@trainingcloud).</description>
    <link>https://dev.to/trainingcloud</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F610134%2Fd548964e-e78f-486d-ba2a-bb2edc1077c8.jpg</url>
      <title>DEV Community: TrainingCloud.io</title>
      <link>https://dev.to/trainingcloud</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/trainingcloud"/>
    <language>en</language>
    <item>
      <title>Drupal 8/9: Adding Alternating Class Names to Views Rows with Twig</title>
      <dc:creator>TrainingCloud.io</dc:creator>
      <pubDate>Thu, 20 May 2021 14:01:22 +0000</pubDate>
      <link>https://dev.to/trainingcloud/drupal-8-9-adding-alternating-class-names-to-views-rows-with-twig-kh1</link>
      <guid>https://dev.to/trainingcloud/drupal-8-9-adding-alternating-class-names-to-views-rows-with-twig-kh1</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You've created a view, and would like each row of the view result to have an alternating class name, like &lt;code&gt;even&lt;/code&gt; and &lt;code&gt;odd&lt;/code&gt;. This is also referred to as 'zebra styles'.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;p&gt;Override the appropriate twig template file and add a bit of twig logic to make the alternating class names happen.&lt;/p&gt;

&lt;p&gt;Spoiler alert: it will only take &lt;strong&gt;a single line of custom code&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;determine the appropriate template file to copy from &lt;code&gt;/core/modules/views/templates&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;copy the file over to &lt;code&gt;/themes/custom/YOUR_THEME/templates/views&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;rename the file, taking into account Drupal's Twig template naming conventions&lt;/li&gt;
&lt;li&gt;add your custom logic to the file&lt;/li&gt;
&lt;li&gt;clear your caches&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;our view is an &lt;em&gt;unformatted list&lt;/em&gt; of &lt;em&gt;articles&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;our view's identifier (machine name) is &lt;em&gt;articles&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 1: copy the right template file into your theme
&lt;/h4&gt;

&lt;p&gt;Since the view's &lt;em&gt;format&lt;/em&gt; is set to &lt;em&gt;unformatted list&lt;/em&gt;, copy &lt;code&gt;/core/modules/views/templates/views-view-unformatted.html.twig&lt;/code&gt; to &lt;code&gt;/themes/custom/YOUR_THEME/templates/views/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Drupal will always check your theme folder first for template files to use. If it doesn't find an appropriate template file, it will fall back on the default ones.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: rename your template file (if appropriate)
&lt;/h4&gt;

&lt;p&gt;If you don't rename your copied template file at this point, any changes you make will apply to &lt;em&gt;all&lt;/em&gt; of your site's views that use the &lt;em&gt;unformatted_list&lt;/em&gt; format setting.&lt;/p&gt;

&lt;p&gt;In you want this template to only apply to your &lt;em&gt;articles&lt;/em&gt; view, rename the copied template to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/themes/custom/YOUR_THEME/templates/views/templates/views-view-unformatted--articles.html.twig&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;→ See also: &lt;a href="https://www.drupal.org/docs/theming-drupal/twig-in-drupal/twig-template-naming-conventions#s-views" rel="noopener noreferrer"&gt;Drupal 8/9 Twig template naming conventions&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3: add your logic
&lt;/h4&gt;

&lt;p&gt;Here's the default, unmodified Twig template file (comments removed):&lt;/p&gt;

&lt;p&gt;→ /core/modules/views/templates/views-view-unformatted.html.twig&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight twig"&gt;&lt;code&gt;&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;endif&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nv"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;rows&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
  &lt;span class="cp"&gt;{%&lt;/span&gt;
    &lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nv"&gt;row_classes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="nv"&gt;default_row_class&lt;/span&gt; &lt;span class="err"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'views-row'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="cp"&gt;%}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt;&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;row.attributes.addClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;row_classes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="cp"&gt;{{&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;row.content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="cp"&gt;}}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;endfor&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your copied tempate file, under &lt;code&gt;default_row_class ? 'views-row',&lt;/code&gt; add the following single line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight twig"&gt;&lt;code&gt;cycle(['even', 'odd'], loop.index0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the complete code becomes:&lt;/p&gt;

&lt;p&gt;→ /themes/custom/YOUR_THEME/templates/views/templates/views-view-unformatted--articles.html.twig&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight twig"&gt;&lt;code&gt;&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;title&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;endif&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nv"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;rows&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
  &lt;span class="cp"&gt;{%&lt;/span&gt;
    &lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nv"&gt;row_classes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="nv"&gt;default_row_class&lt;/span&gt; &lt;span class="err"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'views-row'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nv"&gt;cycle&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'even'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'odd'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nv"&gt;loop.index0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="cp"&gt;%}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt;&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;row.attributes.addClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;row_classes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="cp"&gt;{{&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;row.content&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="cp"&gt;}}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;endfor&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cycle()&lt;/code&gt; is a core twig function that cycles through a given array of values.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;loop.index0&lt;/code&gt; indicates the position (index) of the iterator during the looping, starting at 0. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;→ More info on twig functions: &lt;a href="https://twig.sensiolabs.org/doc/2.x/functions/index.html" rel="noopener noreferrer"&gt;https://twig.sensiolabs.org/doc/2.x/functions/index.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since this template file will be used each time a row of the articles view is rendered, either 'even' or 'odd' will be added to &lt;code&gt;row_classes&lt;/code&gt;, which holds the list of classes to be used in the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; that wraps around the row's &lt;code&gt;{ row.content }&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a id="summary"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Override the default &lt;code&gt;views-view-unformatted.html.twig&lt;/code&gt; template and add your logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Copy &lt;code&gt;/core/modules/views/templates/views-view-unformatted.html.twig&lt;/code&gt; into your theme.&lt;/li&gt;
&lt;li&gt;Rename the template file so it applies only to your target view.&lt;/li&gt;
&lt;li&gt;Add a line of code that cycles between two given class names.&lt;/li&gt;
&lt;li&gt;Clear your caches.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Further resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.drupal.org/docs/theming-drupal/twig-in-drupal/twig-template-naming-conventions#s-views" rel="noopener noreferrer"&gt;Drupal 8/9 Twig template naming conventions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twig.sensiolabs.org/doc/2.x/functions/index.html" rel="noopener noreferrer"&gt;https://twig.sensiolabs.org/doc/2.x/functions/index.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you liked this article, you'll love &lt;a href="https://trainingcloud.io/courses" rel="noopener noreferrer"&gt;our courses&lt;/a&gt; on Drupal 8/9 Site Building, Module Development, and Theme Development. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wav7v6hn30d0q9vfy3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wav7v6hn30d0q9vfy3c.png" alt="TrainingCloud: Drupal and PHP training for (aspiring) developers"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>drupal</category>
      <category>theming</category>
      <category>snippet</category>
      <category>twig</category>
    </item>
    <item>
      <title>Drupal 8/9: Listing all available entity types</title>
      <dc:creator>TrainingCloud.io</dc:creator>
      <pubDate>Wed, 19 May 2021 10:41:01 +0000</pubDate>
      <link>https://dev.to/trainingcloud/drupal-8-9-listing-all-available-entity-types-44o3</link>
      <guid>https://dev.to/trainingcloud/drupal-8-9-listing-all-available-entity-types-44o3</guid>
      <description>&lt;p&gt;Whether you're new to Drupal or you're working on a complex site you didn't build yourself, it's not always easy to keep an overview of the site's available entity types.&lt;/p&gt;

&lt;p&gt;Here are two quick ways to help you gain insight into your Drupal entity types:&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 1. Using Drupal Console
&lt;/h2&gt;

&lt;p&gt;If you have &lt;a href="https://drupalconsole.com/"&gt;Drupal Console&lt;/a&gt; installed, you can run the following anywhere inside your Drupal project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;drupal debug:entity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or its shorter alias &lt;code&gt;de&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drupal de
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--------------------------- --------------- 
  Entity name                 Entity type    
 --------------------------- --------------- 
  comment                     Content        
  contact_message             Content        
  node                        Content        
  block_content               Content        
  menu_link_content           Content        
  file                        Content        
  group                       Content        
  group_content               Content        
  shortcut                    Content        
  taxonomy_term               Content        
  path_alias                  Content        
  user                        Content        
  action                      Configuration  
  base_field_override         Configuration  
  block                       Configuration  
  comment_type                Configuration  
  contact_form                Configuration  
  language_content_settings   Configuration  
  node_type                   Configuration  
  block_content_type          Configuration  
  [...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Option 2: Using a code snippet
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$entity_types&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Drupal&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;entityTypeManager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getDefinitions&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$entity_types&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$entity_type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'group'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$entity_type&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getGroupLabel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="s1"&gt;'class'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$entity_type&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getClass&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="s1"&gt;'provided by'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$entity_type&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getProvider&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Sort alphabetically by key and print result.&lt;/span&gt;
&lt;span class="nb"&gt;ksort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could save this as a standalone php script (let's name it &lt;code&gt;entities.php&lt;/code&gt;, and run it with &lt;a href="https://drush.org/"&gt;drush&lt;/a&gt; or Drupal Console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drush scr path/to/entities.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drupal snippet --file=/full/path/to/entities.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[action] =&amp;gt; Array
    (
        [group] =&amp;gt; Configuration
        [class] =&amp;gt; Drupal\system\Entity\Action
        [provided by] =&amp;gt; system
    )

[base_field_override] =&amp;gt; Array
    (
        [group] =&amp;gt; Configuration
        [class] =&amp;gt; Drupal\Core\Field\Entity\BaseFieldOverride
        [provided by] =&amp;gt; core
    )

[block] =&amp;gt; Array
    (
        [group] =&amp;gt; Configuration
        [class] =&amp;gt; Drupal\block\Entity\Block
        [provided by] =&amp;gt; block
    )

[block_content] =&amp;gt; Array
    (
        [group] =&amp;gt; Content
        [class] =&amp;gt; Drupal\block_content\Entity\BlockContent
        [provided by] =&amp;gt; block_content
    )

[block_content_type] =&amp;gt; Array
    (
        [group] =&amp;gt; Configuration
        [class] =&amp;gt; Drupal\block_content\Entity\BlockContentType
        [provided by] =&amp;gt; block_content
    )

[comment] =&amp;gt; Array
    (
        [group] =&amp;gt; Content
        [class] =&amp;gt; Drupal\comment\Entity\Comment
        [provided by] =&amp;gt; comment
    )

[comment_type] =&amp;gt; Array
    (
        [group] =&amp;gt; Configuration
        [class] =&amp;gt; Drupal\comment\Entity\CommentType
        [provided by] =&amp;gt; comment
    )
[...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Further Reading
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityTypeManager.php/class/EntityTypeManager/9.1.x"&gt;&lt;code&gt;Drupal::entityTypeManager()&lt;/code&gt;&lt;/a&gt; on api.drupal.org.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you liked this article, you'll love &lt;a href="https://trainingcloud.io/courses"&gt;our courses&lt;/a&gt; on Drupal 8/9 Site Building, Module Development, and Theme Development. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oc86zSTF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wav7v6hn30d0q9vfy3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oc86zSTF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wav7v6hn30d0q9vfy3c.png" alt="TrainingCloud: Drupal and PHP training for (aspiring) developers"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Article image: &lt;a href="https://unsplash.com/photos/RLw-UC03Gwc/"&gt;Unsplash / Glenn Carstens-Peters&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>drupal</category>
      <category>php</category>
      <category>programming</category>
      <category>snippet</category>
    </item>
    <item>
      <title>Drupal 8/9: Adding CSS body classes based on the current user's roles</title>
      <dc:creator>TrainingCloud.io</dc:creator>
      <pubDate>Mon, 03 May 2021 11:44:37 +0000</pubDate>
      <link>https://dev.to/trainingcloud/drupal-8-9-adding-body-classes-based-on-the-current-user-s-roles-2o4</link>
      <guid>https://dev.to/trainingcloud/drupal-8-9-adding-body-classes-based-on-the-current-user-s-roles-2o4</guid>
      <description>&lt;p&gt;Sometimes it's useful to add body classes based on the current Drupal user's roles.&lt;/p&gt;

&lt;p&gt;This article shows how to do this by adding or modifying a &lt;code&gt;theme_preprocess_html()&lt;/code&gt; implementation in your &lt;code&gt;.theme&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;No twig modifications required.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: implement &lt;code&gt;theme_preprocess_html()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In your theme's &lt;code&gt;MYTHEME.theme&lt;/code&gt; file, add the following preprocess function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cd"&gt;/**
 * Implements hook_preprocess_html().
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;MYTHEME_preprocess_html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$variables&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;$roles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;\Drupal&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getRoles&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$roles&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$role&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$variables&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'attributes'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'class'&lt;/span&gt;&lt;span class="p"&gt;][]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"role-&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$role&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;→ Be sure to replace &lt;em&gt;MYTHEME&lt;/em&gt; with the name of your own theme.&lt;br&gt;
→ Be sure to clear your caches.&lt;/p&gt;

&lt;p&gt;Assuming the current user has the &lt;em&gt;administrator&lt;/em&gt; role, the body classes for each of the pages this user loads will now also contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;role-authenticated&lt;/li&gt;
&lt;li&gt;role-administrator&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Step 2: add styles
&lt;/h2&gt;

&lt;p&gt;You can now add styles targeted at specific user roles.&lt;/p&gt;

&lt;p&gt;We use this technique to render certain administrative / debugging styles for admin users.&lt;/p&gt;

&lt;p&gt;Here's an example: highlighting images without &lt;em&gt;alt&lt;/em&gt; descriptions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.role-administrator&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
&lt;span class="nc"&gt;.role-administrator&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;""&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;If you liked this article, you'll love &lt;a href="https://trainingcloud.io/courses" rel="noopener noreferrer"&gt;our courses&lt;/a&gt; on Drupal 8/9 Site Building, Module Development, and Theme Development. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wav7v6hn30d0q9vfy3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wav7v6hn30d0q9vfy3c.png" alt="TrainingCloud: Drupal and PHP training for (aspiring) developers"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>drupal</category>
      <category>php</category>
      <category>theming</category>
      <category>snippet</category>
    </item>
    <item>
      <title>Drupal 8/9: Highlighting Views Search Results</title>
      <dc:creator>TrainingCloud.io</dc:creator>
      <pubDate>Wed, 07 Apr 2021 15:07:05 +0000</pubDate>
      <link>https://dev.to/trainingcloud/drupal-8-9-highlighting-views-search-results-4a8i</link>
      <guid>https://dev.to/trainingcloud/drupal-8-9-highlighting-views-search-results-4a8i</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You used exposed filters to filter a view, and you want to highlight certain filter values in the view's results, like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vmqa2cb382p5e6zhqxh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vmqa2cb382p5e6zhqxh.png" alt="Screenshot of a view with certain words highlighted"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might be tempted to try to modify the views search results markup by implementing &lt;a href="https://api.drupal.org/api/drupal/core%21modules%21search%21search.pages.inc/function/template_preprocess_search_result/8.9.x" rel="noopener noreferrer"&gt;template_preprocess_search_result()&lt;/a&gt;, but &lt;strong&gt;this will not work for views&lt;/strong&gt; because views-based searches don't use the built-in search engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;p&gt;One approach is to implement &lt;a href="https://api.drupal.org/api/drupal/core%21modules%21views%21views.theme.inc/function/template_preprocess_views_view_field/8.9.x" rel="noopener noreferrer"&gt;template_preprocess_views_view_field()&lt;/a&gt;, which lets you alter the content that is passed to the template file used for rendering each field in each row of a views result.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Implement &lt;code&gt;template_preprocess_views_view_field()&lt;/code&gt; in such a way that it wraps the specified exposed filter value in &lt;code&gt;&amp;lt;mark&amp;gt;&amp;lt;/mark&amp;gt;&lt;/code&gt; tags.&lt;/li&gt;
&lt;li&gt;add css to give &lt;code&gt;&amp;lt;mark&amp;gt;&amp;lt;/mark&amp;gt;&lt;/code&gt; tags a yellow background.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;Here's an example implementation using a custom module named views_search_highlight.&lt;/p&gt;

&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our view's identifier (machine name) is my_search_demo.&lt;/li&gt;
&lt;li&gt;We're exposing a filter on the body field.&lt;/li&gt;
&lt;li&gt;The exposed filter's identifier is body_value
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// File: modules/custom/views_search_highlight/views_search_highlight.module&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * @file
     * Contains code to implement highlighting exposed views filter values.
     */&lt;/span&gt;

    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Drupal\Core\Render\Markup&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Implements hook_preprocess_views_view_field().
     *
     * This function is called for each field in each row of each view result
     *   (unless overridden by a more specific preprocessor elsewhere).
     */&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;views_search_highlight_preprocess_views_view_field&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$variables&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="cd"&gt;/** @var \Drupal\views\ViewExecutable $view */&lt;/span&gt;
      &lt;span class="nv"&gt;$view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$variables&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'view'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

      &lt;span class="cd"&gt;/** @var \Drupal\views\Plugin\views\field\EntityField $field */&lt;/span&gt;
      &lt;span class="nv"&gt;$field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$variables&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'field'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

      &lt;span class="c1"&gt;// Specify for which view(s) and field(s) you want to enable highlighting.&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$view&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'my_search_demo'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;$field&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'body'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="nv"&gt;$keyword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;\Drupal&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'body_value'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;is_null&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$keyword&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

          &lt;span class="cd"&gt;/** @var \Drupal\views\ResultRow $row */&lt;/span&gt;
          &lt;span class="nv"&gt;$row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$variables&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'row'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

          &lt;span class="c1"&gt;// Retrieve the field's rendered content.&lt;/span&gt;
          &lt;span class="nv"&gt;$content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$field&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;advancedRender&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;__tostring&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

          &lt;span class="c1"&gt;// Apply highlighting to field value.&lt;/span&gt;
          &lt;span class="nv"&gt;$highlighted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;views_search_highlight_apply_highlight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$keyword&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

          &lt;span class="c1"&gt;// Update field output (must be a Markup object, not a string of html).&lt;/span&gt;
          &lt;span class="nv"&gt;$variables&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'output'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Markup&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$highlighted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

          &lt;span class="c1"&gt;// Attach css. Skip this if you're setting the style in your theme's css.&lt;/span&gt;
          &lt;span class="nv"&gt;$view&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'#attached'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'library'&lt;/span&gt;&lt;span class="p"&gt;][]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'view_search_highlight/highlight'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * Search for a given keyword in a given source string and wraps the keyword
     * in &amp;lt;mark&amp;gt;&amp;lt;/mark&amp;gt; tags.
     *
     * @param string $keyword
     *   The keyword to highlight.
     * @param string $source
     *   The content in which to highlight the keyword.
     *
     * @return string
     *   String with highlighted sections (if any).
     */&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;views_search_highlight_apply_highlight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$keyword&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nv"&gt;$highlighted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;str_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$keyword&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;mark&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$keyword&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/mark&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$highlighted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Note 1
&lt;/h3&gt;

&lt;p&gt;Comments like &lt;code&gt;/** @var \Drupal\views\ResultRow $row */&lt;/code&gt; are optional. They are type hints so your code editor knows of which class the objects are an instance, and provide automatic code completion.&lt;/p&gt;

&lt;h3&gt;
  
  
  Note 2
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;views_search_highlight_apply_highlight()&lt;/code&gt; function is very basic. Feel free to modify its logic to suit your needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Note 3
&lt;/h3&gt;

&lt;p&gt;To keep things short I'm not showing the &lt;code&gt;view_search_highlight/highlight&lt;/code&gt; library definition, nor the css file. You can find these in this module's &lt;a href="https://gitlab.com/jpoesen/views-search-highlight" rel="noopener noreferrer"&gt;full source code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Going further
&lt;/h2&gt;

&lt;p&gt;Basic keyword highlighting is now implemented.&lt;br&gt;&lt;br&gt;
Here are some ideas to improve this module:  &lt;/p&gt;

&lt;h3&gt;
  
  
  Make it smarter
&lt;/h3&gt;

&lt;p&gt;You could improve this code by making the replacement function smarter (use word boundaries, allow/enforce partial matches or full matches, etc). If you need some inspiration, have a look at the core search module's &lt;a href="https://api.drupal.org/api/drupal/core%21modules%21search%21search.pages.inc/function/template_preprocess_search_result/8.9.x" rel="noopener noreferrer"&gt;template_preprocess_search_result()&lt;/a&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make it more generic
&lt;/h3&gt;

&lt;p&gt;You could change the logic to always highlight filter values if the filter identifier name starts with "highlight_". &lt;/p&gt;

&lt;p&gt;That way you don't need to modify the code each time you want to have highlighting on exposed filter values in other views.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Implement &lt;code&gt;hook_preprocess_views_view_field()&lt;/code&gt; in a custom module or theme:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieve the search keyword from the request object.&lt;/li&gt;
&lt;li&gt;Retrieve the fully rendered field content.&lt;/li&gt;
&lt;li&gt;Inside the field content, replace the keyword to highlight with the keyword wrapped in &lt;code&gt;&amp;lt;mark&amp;gt;&amp;lt;/mark&amp;gt;&lt;/code&gt; tags.&lt;/li&gt;
&lt;li&gt;Add css to style &lt;code&gt;&amp;lt;mark&amp;gt;&amp;lt;/mark&amp;gt;&lt;/code&gt; with a yellow background.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you liked this article, you'll love &lt;a href="https://trainingcloud.io/courses" rel="noopener noreferrer"&gt;our courses&lt;/a&gt; on Drupal 8/9 Site Building, Module Development, and Theme Development. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wav7v6hn30d0q9vfy3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wav7v6hn30d0q9vfy3c.png" alt="TrainingCloud: Drupal and PHP training for (aspiring) developers"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>drupal</category>
      <category>php</category>
    </item>
  </channel>
</rss>
