• 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:

  • 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.

  • Development

    Only Migrating Published Nodes in a D7 to D8 Migration

    I’m not going into much detail here but hopefully this helps someone.

    If you need to migrate only published nodes you can extend the d7_node plugin and add a condition to the query.

    First, create a file in your module like /modules/custom/mysite_migrate/src/Plugin/migrate/source/MysiteNewsNode.php that extends the d7_node plugin:

  • Development

    Basic HTTP Authentication in Drupal Site Using settings.php

    Here’s a quick and painless way of preventing public access to a Drupal site using settings.php (or settings.local.php).

    I’ve been using this for development and staging sites that I want to keep private.

    If you want this to be available to all settings*.php files you should put this near the top of your settings.php file:

    Then, you can leverage it wherever you’d like. For example, on an Acquia site I might add this to the bottom of settings.php:

    For non-Acquia sites I’d call the function at the bottom of settings.local.php.

  • Tech Tips

    Adding Entity Wrapper Class from Field Value using Display Suite

    NOTICE: This works fine in Drupal 7. For Drupal 8 please note: https://www.drupal.org/node/2863420

    This may not work with paragraph bundles (because you don’t have a Token view mode). Maybe there’s a way, but I don’t have time to mess with it.

    Sometimes as a developer you need to provide your Drupal authors with the ability to choose different variations of a particular content object. For example, imagine you have a Callout paragraph bundle and you want the author to choose between a Dark or a Light colorscheme; if the user chooses Light, the font colors appear dark over a light background. To achieve this you need a dark or light class on the entity wrapper based on whichever the author chose; this would let you write CSS to target each color scheme. Using Display Suite this is an easy problem to solve and doesn’t require any preprocess code or other hand-coded solution.

    This solution requires Token and Display Suite. You will need to enable the ui modules from these, as well as ds_extras to give you the CSS class functionality.

  • Development

    Friendly URL for Views Contextual Filter on a Boolean Field

    I have a story content type that has a corresponding Views listing page. The stories can be flagged as Featured through a checkbox/boolean field ( field_story_featured ). The stories also have a Story Type value ( field_story_type ). The views listing page needs to have the following links at the top:

    Screenshot_2017-02-08_15-05-57

    The URL structure for the Views page makes the most sense as follows (purple = story type contextual filter, orange = featured contextual filter):

  • Tech Tips

    Backing up a File Before Overwriting It (in Drupal 7)

    I’m communicating with a 3rd party SOAP API to pull some data into a CSV file. The CSV file will be used by Feeds and/or Migrate so it’s important to have the file named exactly the same for each run. I want the generated CSV to always be named “mdb_interests.csv” but I want to “backup” the existing “mdb_interests.csv” to “mdb_interests_N.csv” if one already exists. This will ensure that we always have the latest data in “mdb_interests.csv” but will have backups from all previous API pulls. Using some built-in Drupal functions it’s pretty simple:

  • Development

    Adding an Inline Style Attribute to an Entity in Drupal 7

    The point of this post is to illustrate adding inline styles without creating template files. Normally we add classes via $vars['classes_array']  but sometimes inline styles are required.

    This code isn’t too useful as written (make all text red for all entity types, really?), but drop in a conditional to affect specific entity types and replace the styles with something useful like setting a background image URL from a field within the entity and you have a handy snippet.

     

  • Development

    Entity Reference Referrer – Getting Entity Reference’s Parent/Referrer Entity ID and Type

    Out-of-the-box, the Entity Reference module doesn’t provide a way to access the referrer entity’s fields from within the referenced entity. I have two ways of doing this. The first is to patch the entityreference module (not ideal unless this patch went out to the community via proper channels; perhaps I should get on that?). The second is to mimic entityreference’s “Rendered entity” field formatter but modify it to include the referrer information. I recommend the second method for now.

  • Development

    Appending Language to Menu Items in Menu Admin (Drupal 7)

    If you are working with a Drupal 7 menu that contains menu items across multiple languages it is a little cumbersome to see them all on the same screen. Right now we have all of the translations in place (3 nodes per piece of content), but we haven’t translated the titles yet. It’s very hard to see which items will appear for which languages. Imagine if we add 75 more menu items!

    2016-08-31_15-01-44