Hiding Country from an Address Field’s Output in Drupal 8

There are two field formatters available on an Address field in Drupal 8:

The Plain formatter uses a Twig template file but it would take some work to override with the proper markup. The Default formatter doesn’t use a Twig template file so you cannot simply override via Twig.

If your goal is to simply hide the country value from the output you can use hook_preprocess_field to alter the country value. As it turns out, unsetting the country field doesn’t help, but setting the value to an empty string does work.

Here’s an example (in a module; you can put in your .theme file if you’d prefer the setting to be at the theme level).

 

Tagged with: , ,
Posted in Development

Tideways and XHGui in Valet+ (Valet Plus)

This post is for folks who are already familiar with PHP, command line, Valet, Tideways and XHGui.

You can see some explanation and screenshots of Tideways and Xhgui in my Tideways and Xhgui using Dev Desktop post if you need a bit of an introduction.

I’ve successfully gotten Tideways running with Acquia Dev Desktop, Lando (haven’t shared publicly yet), and now Valet+. This post illustrates the process I used to get Tideways, Xhgui, and their dependencies running on my Mac for use in Valet+ sites. Read more ›

Tagged with: , , , ,
Posted in Development

Access Entity Properties in a Field Twig Template

This doesn’t need much of an explanation. You can use any entity methods as far as I can tell.

Here’s an example showing how to get the node ID in a field template:

 

Tagged with: ,
Posted in Development

Modifying Rows During a Drupal 8 CSV Migration

Migrate Source CSV is currently the source plugin of choice for doing a CSV-to-Drupal migration with the Migrate API in Drupal 8. In this post I will demonstrate how to manipulate the CSV data in realtime during the migrate:import operation. You can think of this as the equivalent to prepareRow() that you have seen elsewhere, like my blog post Extending the Migrate Plus JSON Parser in Drupal 8.

Please make sure you have a working migration before you begin; it’ll make things easier to troubleshoot if you know you had a good starting point. Read more ›

Tagged with: , , , ,
Posted in Development

404 Error Serving a /libraries page in Drupal 8

It took some time to figure out why I kept seeing a 404 error (page not found) on http://www.mysite.com/libraries. The issue seemed to only present itself on our Acquia environments. Ultimately I realized that there was a conflict due to having a “libraries” directory in the document root of the site (e.g., /docroot/libraries).

It was a quick fix after realizing the cause of the problem; I added this to my .htaccess file, above the other index.php rewrite rules.

The reason this wasn’t happening on my local machine is because I’m using nginx locally for this particular website; the .htaccess file isn’t used.

Tagged with: , ,
Posted in Development

Joining Strings in a Drupal Views Field using Twig

I have two optional fields on a Drupal 8 content type: City and State. Both are rendered as simple text.

In a fields-based View I wanted to show the field output as “Portland” or “Portland, OR”, or “OR”.

First, I added the two fields, State then City (the order is important). Next I excluded the State field from display.

Finally, I opened the Rewrite Results pane of the City field and checked the Override the output of this field with custom text checkbox.

After several attempts at using Twig’s joinreplacespaceless, and more, I landed on this simple solution as the rewrite text:

Note the whitespace modifiers (hyphens). These are the key to getting  Portland, OR  instead of   Portland , OR .

Also, we cannot just use  {{field_member_city}}, {{field_member_state}} because we could end up with  , OR if City is empty.

Tagged with: , , ,
Posted in Development

Drupal + Twig: Render Taxonomy Terms and Comma-Separated List

Here’s a snippet that renders the terms in a multi-value taxonomy reference field (in a node template) as plain text items separated by commas. Yes, you could get the string into the desired format using PHP, but I wanted to try to do it with only Twig. The result is moderately readable.

Twig Code:

For a taxonomy term reference field called field_pub_tr_res_committees

Output:

If 1 value: My first term Read more ›

Tagged with: , ,
Posted in Development

Migrating Specific Fields in a D8 Migration

I recently needed a way to update the value of a single field of a D7 to D8 node migration.

The client was already updating the migrated content so I had to be careful with what I migrated.

The entity destination plugin has a wonderful configuration option that allows you to specify which fields you want to migrate. Search the code here for overwrite_properties.

Here’s how I used this to ignore every field except two:

I tested this by editing a node’s title, subtitle, field_tax_groups, and field_tags values. I ran the migration and the edits were preserved on the title and subtitle fields as expected.

Tagged with: , ,
Posted in Development

Migrating a Drupal 7 User Profile2 Image Field to a Drupal 8 User Image Field

Consider this scenario:

  • Drupal 7 site contains Profile2 profile called person with image field called field_oldsite_person_image.
    • Users each have a profile2 profile associated with their account.
  • Drupal 8 site has an image field called field_newsite_user_photo on the user entity itself.

If you need to migrate the image field from the Drupal 7 profile to the Drupal 8 user entity you can extend the d7_user plugin and query for the additional field data. I’m executing an extra query for each user (because the code is in the prepareRow() method), rather than attempting to pull all of the profile data as part of the query in the query() method of the class.

First, create a file in your module like /modules/custom/mysite_migrate/src/Plugin/migrate/source/MysiteUser.php that extends the d7_user plugin: Read more ›

Tagged with: , , , ,
Posted in Development

Quick Tips for a Drupal 7 to Drupal 8 Migration

Here are a few quick tips for a Drupal 7 to Drupal 8 migration. I will add more over time; admittedly it’s pretty lame right now!

For general Drupal 8 Migrate API tips, you may also want to check out my other post: Drupal 8 Migrate – Tips and Tricks

Source Plugins

If you’re using a source plugin like d7_user or d7_node it will help you understand what’s happening if you view the source code for the plugin. Pay close attention to the query(), prepareRow(), and fields() methods. Read more ›

Tagged with: , , ,
Posted in Development