Migration from block_class

Created on 19 October 2023, about 1 year ago
Updated 4 January 2024, 12 months ago

Problem/Motivation

We've used block_class, with patches for third_party_settings, to add classes to blocks inside layout builder on a bunch of different sites. Block Class 2.x works substantially differently than it did in 1.x, and since discovering this module, we've been using it on all of our newer sites. We would like to migrate our existing sites using block_class to this module.

Steps to reproduce

The old block_class module stores the block class in the third_party_settings, and requires the core patch from ✨ Support third party settings for components within a section Needs work .

Here's an example diff from a manually changed layout builder section:

             weight: 1                                                                                                                                              
-            additional: {  }                                                                                                                                       
-            third_party_settings:
-              block_class:
-                classes: 'header-one moss-text'
+            additional:
+              component_attributes:
+                block_attributes:
+                  id: ''
+                  class: 'header-one moss-text'
+                  style: ''
+                  data: ''
+                block_title_attributes:
+                  id: ''
+                  class: ''
+                  style: ''
+                  data: ''
+                block_content_attributes:
+                  id: ''
+                  class: ''
+                  style: ''
+                  data: ''
+            third_party_settings: {  }
           95a17eb9-1883-4b46-9059-15df9e3f1d2a:
             uuid: 95a17eb9-1883-4b46-9059-15df9e3f1d2a
             region: second

Proposed resolution

Create a drush command to migrate the third_party_settings.block_class.classes value to additional.component_attributes.block_attributes.class for all layout builder templates.

Remaining tasks

We will probably create such a drush command in the next couple weeks, so we can upgrade block_class to the 2.x release which has Drupal 10 support but does not currently work with Layout Builder. Main question is, would this be useful enough for anyone else to include with this module?

User interface changes

New drush command.

API changes

None.

Data model changes

None.

✨ Feature request
Status

Closed: won't fix

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States freelock Seattle

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @freelock
  • πŸ‡ΊπŸ‡ΈUnited States Chris Burge

    @freelock - I think an upgrade path would be useful for other Drupalers, as well. Take a look at layout_builder_component_attributes_uninstall() in layout_builder_component_attributes.install. If third-party settings for components is ever committed in core, my plan is to adapt this code to move the settings from additional to third-party settings.

    In terms of committing an upgrade path to this module, I don't see that happening, unfortunately. Neither the core issue ✨ Support third party settings for components within a section Needs work nor the Block Class issue ✨ Integration with Drupal core's new Layout Builder Needs review have seen code merged. Plus, for the upgrade path to work, a site would need the core patch. That said, the code could be uploaded to this issue. A site owner would only need to run it once.

  • πŸ‡ΊπŸ‡ΈUnited States freelock Seattle

    Circling back to this, here's code we've used to successfully move block_class classes to layout_builder_component_attributes, for anyone who might need it, wired into hook_update_N() in a custom module install file:

    /**
     * Move block_class in custom layouts to layout_builder_custom_attributes.
     */
    function mymodule_update_9035(&$sandbox) {
      // we have both taxonomy term and node updates to do:
      $query = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->getQuery();
      $query->condition('layout_builder__layout', '%block_class%', 'LIKE')
        ->accessCheck(FALSE);
      $results = $query->execute();
    
      foreach ($results as $tid) {
        $term = \Drupal\taxonomy\Entity\Term::load($tid);
        _cgrace_site_bc_to_lbca($term);
      }
      // Now nodes
      $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
      $query->condition('layout_builder__layout', '%block_class%', 'LIKE')
        ->accessCheck(FALSE);
      $results = $query->execute();
    
      foreach ($results as $nid) {
        $node = \Drupal\node\Entity\Node::load($nid);
        _mymodule_bc_to_lbca($node);
      }
    }
    
    function _mymodule_bc_to_lbca(\Drupal\Core\Entity\ContentEntityInterface $entity) {
      $changed = FALSE;
      foreach($entity->layout_builder__layout as $delta => $item) {
        /** @var \Drupal\layout_builder\Section $section */
        $section = $item->section;
        $array = $section->toArray();
        foreach ($section->getComponents() as $uuid => $component) {
          $classes = $component->getThirdPartySetting('block_class', 'classes');
          if ($classes) {
            $additional_settings = [
              'block_attributes' => [
                'id' => '',
                'class' => $classes,
                'style' => '',
                'data' => '',
              ],
              'block_title_attributes' => [
                'id' => '',
                'class' => '',
                'style' => '',
                'data' => '',
              ],
              'block_content_attributes' => [
                'id' => '',
                'class' => '',
                'style' => '',
                'data' => '',
              ],
            ];
            $component->set('component_attributes', $additional_settings);
            $component->unsetThirdPartySetting('block_class', 'classes');
            $changed = TRUE;
          }
        }
      }
      if ($changed) {
        $entity->save();
        $message = t('Migrated block class on %type - %id',[
          '%type' => $entity->getEntityTypeId(),
          '%bundle' => $entity->bundle(),
          '%id' => $entity->id(),
        ]);
        \Drupal::messenger()->addMessage($message);
        \Drupal::logger('cgrace')->notice($message);
      }
    }
    
    
  • πŸ‡ΊπŸ‡ΈUnited States Chris Burge

    @freelock - Thanks for sharing!

  • Status changed to Closed: won't fix 12 months ago
  • πŸ‡ΊπŸ‡ΈUnited States Chris Burge

    Since this code won't be committed, I'm going to close as "won't fix", but the issue will still show up in search. It can also be linked from the issue over in Block Class for other looking to make the switch.

Production build 0.71.5 2024