-
Make a Field Label “visually-hidden” in Drupal 8
Using the Form API you can add a
visually-hidden
class to a field using the “#title_display” property as shown here:12345678/*** Implements hook_form_FORM_ID_alter().*/function mymodule_form_views_exposed_form_alter(&$form, $form_state, $form_id) {if ($form['#id'] == 'views-exposed-form-search-page-1') {$form['keys']['#title_display'] = 'invisible';}} -
Drupal 8 Search API Tips
Here are some quick tips for configuring Search API for Drupal 8.
Easy Indexing
If you’re like me you rely heavily on Display Modes (aka View Modes) for your entity types. The most convenient way to get Search API to index your data is to use the Search Index view mode on any entity types you will index. If you’re using Display Suite you may want to choose a layout that doesn’t include authoring information, published date, etc. Just fill this view mode, in each entity type, with the fields you want to index. If you plan to use this to drive the output, you should also hide labels and do whatever else will ensure clean output; more on this later.
I suggest indexing the Title (in the case of content types) separately. More on that in the Boost notes below.
-
Manually Checking Path-based Visibility in Drupal 8
This mimics the path visibility settings on block configurations.
1234567$current_path = \Drupal::service('path.current')->getPath();$patterns = "/user\n/user/login\n/user/password";$match = \Drupal::service('path.matcher')->matchPath($current_path, $patterns);if ($match) {$this->doSomething();} -
Tideways and Xhgui using DevDesktop and Docker
THIS POST IS UNFINISHED. Use at your own risk. I needed to share with a colleague, so I’m just getting it out into the world. Your Mileage May Vary!
I’ve been working on some large Drupal 8 migrations and have been wanting to profile them for some time due to a few migrations taking far more time than I expected. Acquia DevDesktop, which I happen to be using for this site, offers xhprof in the older PHP environments, but I wanted to get something setup in PHP 7.
For PHP 7 the recommendation is to use Tideways; it’s a modern fork of xhprof. After collecting (tracing/profiling) the data with Tideways you need a way to analyze the data. I used a Docker environment to get Xhgui running. Here’s what a few xhgui screens look like. The best part is that nearly everything is clickable so you can drill down to figure out what’s slow, and why!
-
Block Caching “Gotcha” in Drupal 8
I recently had a fight with the Block system in Drupal 8. To be brief, if you’re trying to disable caching on a block, make sure to set the #cache element regardless of whether the block has output or not.
This does not work (empty result gets cached):
1234567891011121314151617181920212223242526/*** {@inheritdoc}*/public function build() {$output = [];$user_id = \Drupal::routeMatch()->getRawParameter('user');$current_user_id = \Drupal::currentUser()->id();if ($user_id == $current_user_id) {$output = ['#theme' => 'item_list','#list_type' => 'ul','#items' => [Link::fromTextAndUrl(t('Edit profile'), Url::fromRoute('entity.user.edit_form', ['user' => $user_id])),Link::fromTextAndUrl(t('Logout'), Url::fromUserInput('/user/logout')),],'#wrapper-attributes' => ['class' => 'member-utilities'],'#cache' => ['max-age' => 0,],];}return $output;}This does work (nothing gets cached, as desired):
123456789101112131415161718192021222324/*** {@inheritdoc}*/public function build() {$output = ['#cache' => ['max-age' => 0,],];$user_id = \Drupal::routeMatch()->getRawParameter('user');$current_user_id = \Drupal::currentUser()->id();if ($user_id == $current_user_id) {$output['#theme'] = 'item_list';$output['#list_type'] = 'ul';$output['#items'] = [Link::fromTextAndUrl(t('Edit profile'), Url::fromRoute('entity.user.edit_form', ['user' => $user_id])),Link::fromTextAndUrl(t('Logout'), Url::fromUserInput('/user/logout')),];}return $output;} -
Send email after Migrate Import in Drupal 8
Here’s an example of a barebones event subscriber to send an email when a Drupal migration finishes importing. The success vs failure logic isn’t working correctly, but you’ll get the gist. If you come up with a solution for this please contact me; I don’t have time to work on it at the moment.
The file structure looks like this:
mymodule.services.yml
12345services:mymodule_migrate.subscriber:class: Drupal\mymodule_migrate\EventSubscriber\MymoduleMigrateSubscribertags:- { name: event_subscriber } -
Python Faker with the Fake App (Mac OS)
Fake App is a fantastic way to automate form filling, especially for folks without coding skills. The click-and-drag to target an element lets you quickly build your macros.
Python Faker is a utility library that I leverage with tools like Fake App, Alfred, Keyboard Maestro, etc. to generate realistic dummy text. This blog post shows one method of how to get Python Faker data into Fake App; I imagine there might be a simpler way but this works well and is fairly intuitive.
The idea is to just write the Faker output to a file and read that file into a variable for use in Faker. With this method you could even store variables for re-use. For example, if you had three fields: First Name, Last Name, and Full Name, you could easily store the first and last names to separate files and use both values for the Full Name field. With the power of Faker your dummy text options are extensive; you can generate very realistic values for your form filling. See all of the “providers” here.
This example below took just minutes to setup (and it was the first time I’ve used this technique).
-
Setting up xdebug for PHP 7 in Acquia DevDesktop
The Acquia DevDesktop help page says:
The PHP 7 version currently included with Acquia Dev Desktop does not currently include Xdebug. You can download an updated version of Xdebug here .
Here are the actual steps I used. YMMV.
123456789101112131415161718192021cd /Applications/DevDesktop/php7_0/extwget http://xdebug.org/files/xdebug-2.5.5.tgztar xvf xdebug-2.5.5.tgzrm xdebug-2.5.5.tgzcd xdebug-2.5.5/Applications/DevDesktop/php7_0/bin/phpize./configure --enable-xdebug CC="gcc -arch i386" CXX="g++ -arch i386" -with-php-config=/Applications/DevDesktop/php7_0/bin/php-configmakemv modules/xdebug.so ../cd ..rm -rf package.xml xdebug-2.5.5# Edit /Applications/DevDesktop/php7_0/bin/php.ini# Add these lines:zend_extension="/Applications/DevDesktop/php7_0/ext/xdebug.so"xdebug.remote_enable=1xdebug.remote_log=/tmp/xdebug.log# Restart DevDesktop services -
Drupal 8 Migrate Process Plugin: migration_lookup_target_ids
I was having some difficulties using the migration_lookup process plugin. I wrote this plugin so that I could make sense of how the migration lookup was working and the data that it was returning. The plugin ended up being something that I use after every migration_lookup as it seems to ensure error-free lookups. YMMV.
-
Drupal 8 Migrate – Tips and Tricks
This is an ongoing brain dump of quick tips for working with Drupal 8 Migrate. These items may seem true to me as I’m writing them today, but I’m constantly being reminded that things aren’t always as they seem with Migrate. It’s a wonderful system but can be tricky to understand. I recommend reading through my other Migrate-related posts, especially the post on debugging.