My HTML, or what I thought was HTML, was actually generating an object of type Twig_Markup rather than a string, and therefore wasn't passing json validation. Hmm. raw didn't work. But Twig_Markup has a magic method, __toString() which is called, magically, when Twig_Markup is printed. So, I just had to invoke that directly. Which was as easy as: {% set buttonHtml = buttonHtml.__toString(buttonHtml) %) Happy coding! References: Twig_Markup API docs…
Read more about Casting Twig_Markup to string in Twig◆
I had to do a little RTFMing today, and so I thought I'd post about it. First of all, this is how you set up PhpStorm to use ES6 eslint settings. You may find it useful Is the linter getting in your way? The first way to override an eslint setting is inline, disabling it on a one-off basis. If you truly need a new .eslintrc.json file, then you should probably extend the one from Drupal core. Here's how the linter looks for eslint files. TL;DR: like .gitignore files. Finally, here's how the extends property of eslintrc.json…
Read more about Overriding Drupal 8's .eslintrc.json File in Your Theme With Extends◆
You may not know this, but support for ES6 was added in Drupal 8.4. It wasn't in the release notes, but I was delighted to learn of it. You have probably landed here because you have gotten the error: Cannot find module 'eslint-config-airbnb'. If you want to see ES6 linting in PHPStorm, you'll need to do a couple of things. (In order to actually use ES6 in your theme, you'll have to configure your own build transpiling task, which is out of scope here…
Read more about Drupal ES6 Linting in PhpStorm. Or, PhpStorm Drupal Error: Cannot find module 'eslint-config-airbnb'◆
It can be hard to tell why a mixin fails in a sass file. After all, if the mixin isn't included in your dependencies automatically as it was before, grepping for it in the files won't turn up anything. I was using a child theme of Classy, running Drupal 8. It turns out that this particular generically-named mixin comes from susy 2.x, having been removed in susy 3.x. I fixed this by pinning the susy version number in package.json…
Read more about Error: no mixin named container or border-box-sizing◆
I had a very strange problem wherein I was not able to affect variables being passed to views-view-grid--my_view.html.twig. function mytheme_preprocess_views_view(&$vars) { if ($vars['view']->id() == 'myview') { $vars['amaaaazingVar'] = '!'; } } Yet, sadly, there was no amaaaazingVar to be found in views-view-grid--my_view.html.twig when I did {{ kint() }}. What did I do? I set a breakpoint in the twig template using this methodology and then looked back through to see what preprocess functions were being called by the theme. And yes... my preprocess function was called. But the variables weren't getting saved. Strange. Why were my variables clobbered…
Read more about D8: Variables in template_preprocess_views_view not passed to twig◆
On the method of disabling a block without recourse to path…
Read more about D8: Disable Block Programmatically on Just One View or Node◆
On the method of creating custom breadcrumbs: simply crumble stale bread and dip battered eggs into your bowl. But in Drupal…
Read more about Drupal 8.3: Create Programmatic Custom Breadrumb◆
I had a devil of a time figuring out where captioning was coming from on my entity embeds. Another team member had set it up, and I was just kind of baffled as to why captions were being offered on my entity embed forms. It turns out that if you turn on captioning, you just get it for free on all of your embeds, and it's not configurable. But... I don't want it for free! Bah. Humbug. Here's how you turn off captioning using hook_form_alter()…
Read more about 8: Disable Caption on Specific Entity Embed Types in CkEditor◆
All of a sudden, on a commit which had nothing to do with drush as far as I can tell, drush stopped working on platform.sh. I got an error like this: env: drush: No such file or directory Which is to say, "no drush here." But why? It's added in composer. Nothing changed. I ran platform drush-aliases and refreshed them. Stop asking why. Here's how I fixed it. Turns out that drush still exists on the server, even though which drush led to nothing. Not in the path? Unclear. Here's how I fixed it. Or worked around it…
Read more about Platform.sh drush alias stops working◆
Sometimes, you've got a Drupal twig function that doesn't exist in PatternLab, and it prevents PatternLab from building. You get an "Unknown filter" error. What you need to do is create a Twig stub that does either what you need it to, or nothing at all. In emulsify, twig functions exist at emulsify/components/_twig-components/filters. For example, to create a typogrify filter, create a file called typogrify.filter.php…
Read more about Add a New Twig Function in Patternlab◆
Views has a setting to exclude the current nid from the URL from the listing one is currently viewing. This is essential when you have, say, a list of related nodes that are are defined by a category that includes the current node. If you don't exclude the current node, your current node will be listed in the "related content" block on itself. Well, obviously one is related to oneself, one thinks. First, I tired to accomplish this through the UI with these steps, below. Add contextual filter: Get content ID from URL: Exclude results: Perfect! Except, wait…
Read more about 8: Programatically Filter Current Nid from Views Block Listing◆
So, your debug output isn't working. You checked to make sure you followed all the steps here. But still... no theme debug output. WTF? Try turning on and off twig caching. For me, it seemed something was stuck or whatever. First turn it on: twig.config: debug: true auto_reload: true cache: true Then do drush cr. Then turn it off: twig.config: debug: true auto_reload: true cache: false Then do drush cr. Sometimes the code that makes Drupal run can kind of pile up and get stuck, like a logjam in a river. This just clears the blockage. Just kidding…
Read more about Twig/Theme Debug Not Working Even Though Set◆
It turns out that image style tokens are an easter egg of the regular Token module, ported in from the Imagecache Token module on this ticket. Image style tokens don't show up in the media browser, and so you sort of have to guess at how to use them. I figured it out by studying the merged commit that added the functionality…
Read more about Drupal 8: How to Get Image Style Tokens for Metatags◆
Pluralizing and singularizing words got very easy with the inclusion of the Doctrine Inflector class in Drupal 8 core. use Doctrine\Common\Inflector\Inflector; $pluralized = Inflector::pluralize($bundle_singular); $singularized = Inflector::singularize($bundle_singular); You can read more about the Doctrine Inflector class here…
Read more about How to Pluralize and Singularize a Word in Drupal 8◆
You never know when you're going to encounter data ravaged by clients, cast aside and mauled, an antelope passed by lions to jackals. But fortunately, data is much easier to repair than a gaping wound. Here's the SQL query I'd use to replace non-breaking spaces with regular spaces. You'll need to replace it with whatever your search term is. The queries target both node_revision__body and node__body tables using SQL REPLACE and LIKE…
Read more about SQL Search and Replace in Drupal 8 Body Field◆
Here's the controlling issue for adding timezone support to Drupal core. As of now, August 1st, 2017, it doesn't appear to be ready. Instead, I just added a timezone field to the node using the Timezone module, then appended it to the date range on field preprocess. This won't work for you if you have multiple dates on an entity with different timezones (you might want to try the patch linked above), but it will fit most use cases…
Read more about Drupal 8.4x: Add Timezone to Date or DateRange Field◆
Over the last several days, I've railed in my head against the matroshka structure of Drupal entities. But when I tried to break out some helper functions, and when I realized what each of the different properties each entity had, I realized the complexities of entities were warranted. Life is complex, and thus Drupal is complex. Entity references load a Media Entity, which load an Image from an image field, which load a File. The alt and title are on the Image, while the file URI is on the file…
Read more about Drupal 8: Get URI, Title, and Alt from Media Entity Reference Item◆
This is pretty simple, but it took me a minute, because I kept trying to use the base Entity class, like an angry baby trying to remove a clenched fist from a bottle. use Drupal\media_entity\entity\Media; Media::load($media_id)…
Read more about Get Drupal 8 Media Entity Object from ID◆
This function gets all the Media ID's from the database whose names begins with a specific string. I'm using this for an array of default images. You can see I'm using both drupal_static() and \Drupal::cache(). Necessary? I would bet \Drupal::cache() goes to the database to ask for information (unless you're using Memcache or Redis, that is), and thus in a view with multiple content listings calling this function drupal_static() will be faster, because that saves to a PHP variable. Overkill for a little query like this? OK, sue me. I love performance. Here's the code…
Read more about Drupal EntityFieldQuery, drupal_static(), \Drupal::cache() Example◆
Zebras. (I just wanted to use my "z" illumination.) In general, it's not necessary to add a timezone to both start and end dates. But if you modify the date format, that's exactly what will happen. So how do we add the site timezone just to the end of the DateTimeRange? The solution uses mytheme_preprocess_field() to append the timezone abbreviation to the end date, producing output like: 8/2/17 5:15 PM - 8/2/17 10:15 PM EDT…
Read more about Append Site Timezone to End Date in DateRange in Drupal 8◆
Another day learning Drupal 8, but today is troubling. Here's where I'm at. I'm theming search results, and I need to load field values to pass to twig templates—specifically a styled url for img src. Entities values are not loaded on search results, only little snippets, so I have to load them the hard way, because we want images and nice theming on our search results…
Read more about Get File URI from Media Entity Reference Item in Drupal 8 OR Wrong File Loaded◆
Well, that's because you can't. For whatever reason. I can't modify the variables in search-result.html.twig like so: function mytheme_preprocess_search_result(&$variables) { $variables['result']['testvar'] = 'beeeg test!'; $variables['result']['title'] = 'AMAAAAZING NEW TITLE!!!'; } Hmm. Okay. But I can access testvar like so in the twig file: {{ result.testvar }} And if I really want to change the title of my search results, I can do this: function mytheme_preprocess_item_list(&$vars) { foreach ($vars['items'] as &$item) { $res =& $item['value']['#result']; $res['title'] = 'bleaaargh!' ; } } And now all titles are set to 'bleaaargh!' as they should be…
Read more about Can't modify title in search-result.html.twig with template_preprocess_search_result()◆
Quite the problem—Drupal config management on local and prod. Drupal 8 can be punctilious: it will simply refuse to work if there's a mismatch between database and config objects on the filesystem. So... how do we manage two sets of configurations—one for local, and one for production? The Problem I have modules like varnish installed on production that shouldn't be enabled on local, and modules that are enabled on local that shouldn't be enabled on production. I have uninstalled varnish_purger on local…
Read more about Config Management Roundup◆
Nobody likes iframes. That's because you can't style their innards, and they aren't responsive... or are they?!?! The first thing to know about here is the padding height hack. This allows you to set the height of an object relative to the width, because while height is always as a percentage of the container, padding height is as a percentage of width. So all you have to know is the ratio of height to width and you can make a thing that responsively scales. My first attempt was to use javascript to get the viewport height from the iframe…
Read more about Responsive iframe in Drupal 8 from Client Embed Code◆
Often one finds oneself needing to parse HTML. Back in the day, we used regexes, and smoked inside. We didn't even know about caveman coders back then. Later, we'd use SimpleHtmlDom and mostly just swore when things didn't quite work as expected. But now, we can use PHP's DomDocument, and in Drupal we create them using Drupal's HTML utility. At the top of your file place: use Drupal\Component\Utility\Html; Now, I'm looking for an iframe embed in my HTML, and I want the src, width, and height…
Read more about Parse HTML in Drupal 8 to Get Attributes◆
Personally, I feel overwhelmed looking at the ocean of metatags presented on the node edit page when the metatag module is installed. How much more overwhelmed would a regular ol' user feel? I created a small modules to hide the extra form elements from the metatag module: metatag_limit.module…
Read more about Remove Extra Metatags on Node Form, Drupal 8