- 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); } }
- Status changed to Closed: won't fix
12 months ago 1:48am 4 January 2024 - πΊπΈ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.