layout_builder_component_attributes-1.1.0/layout_builder_component_attributes.install

layout_builder_component_attributes.install
<?php

/**
 * @file
 * Provides install, uninstall, and update functions.
 */

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;

/**
 * Implements hook_requirements().
 */
function layout_builder_component_attributes_requirements($phase) {
  $requirements = [];

  if (!class_exists('CssLint\Linter')) {
    $requirements['lbca_php_css_lint']['title'] = t('Layout Builder Component Attributes');
    $requirements['lbca_php_css_lint']['description'] = t('The neilime/php-css-lint library is required. See README.md.');
    $requirements['lbca_php_css_lint']['severity'] = REQUIREMENT_ERROR;
  }

  return $requirements;
}

/**
 * Implements hook_uninstall().
 */
function layout_builder_component_attributes_uninstall() {
  // Get all entity view displays.
  $view_displays = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->loadMultiple();

  // Keep track of entity types with Layout Builder-enabled bundles.
  // The bundles themselves aren't needed.
  $lb_entity_types = [];
  // Loop through entity view displays, sections, and components.
  foreach ($view_displays as $display) {
    if (!$display instanceof LayoutBuilderEntityViewDisplay) {
      continue;
    }
    if ($display->isLayoutBuilderEnabled()) {
      $third_party_settings = $display->getThirdPartySettings('layout_builder');

      // Only track the entity type if overrides are enabled.
      if ($display->isOverridable()) {
        $lb_entity_types[] = $display->getTargetEntityTypeId();
      }

      foreach ($third_party_settings['sections'] as $section) {
        $components = $section->getComponents();
        foreach ($components as $component) {
          // Remove 'component_attributes' from 'additional'.
          $additional = $component->get('additional');
          unset($additional['component_attributes']);
          $component->set('additional', $additional);
        }
      }
      $display
        ->setThirdPartySetting('layout_builder', 'sections', $third_party_settings['sections'])
        ->save();
    }
  }
  $lb_entity_types = array_unique($lb_entity_types);

  // Only track the entity type if overrides are enabled.
  foreach ($lb_entity_types as $entity_type_id) {
    $entity_ids = \Drupal::entityQuery($entity_type_id)
      ->accessCheck(FALSE)
      ->exists(OverridesSectionStorage::FIELD_NAME)
      ->execute();

    // Load entities.
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $entity_storage */
    $entity_storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
    $entities = $entity_storage->loadMultiple($entity_ids);

    // Loop through entities.
    foreach ($entities as $entity) {
      if ($entity instanceof FieldableEntityInterface && $entity->getEntityType()->isRevisionable()) {
        // Load all revision IDs for entity.
        $revision_ids = $entity_storage->getQuery()
          ->accessCheck(FALSE)
          ->allRevisions()
          ->condition($entity->getEntityType()->getKey('id'), $entity->id())
          ->execute();

        // Loop over revisions.
        foreach (array_keys($revision_ids) as $revision_id) {
          $revision = $entity_storage->loadRevision($revision_id);
          _layout_builder_component_attributes_uninstall_process_entity_or_revision($revision);
        }
      }
      else {
        _layout_builder_component_attributes_uninstall_process_entity_or_revision($entity);
      }
    }
  }

}

/**
 * Processes both revisions and entities.
 *
 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
 *   The entity containing each layout override.
 */
function _layout_builder_component_attributes_uninstall_process_entity_or_revision(FieldableEntityInterface $entity) {
  // Loop through each section and component.
  if ($entity->hasField(OverridesSectionStorage::FIELD_NAME)) {
    /** @var \Drupal\layout_builder\Field\LayoutSectionItemList|false $layout_field */
    $layout_field = $entity->get(OverridesSectionStorage::FIELD_NAME);
    if ($sections = $layout_field->getSections()) {
      foreach ($sections as $section) {
        $components = $section->getComponents();
        foreach ($components as $component) {
          // Remove 'component_attributes' from 'additional'.
          $additional = $component->get('additional');
          unset($additional['component_attributes']);
          $component->set('additional', $additional);
        }
      }
      $layout_field->setValue($sections);
      $entity->save();
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc