You are here

function layout_library_entity_view_display_update in Layout builder library 8

Implements hook_ENTITY_TYPE_update().

1 call to layout_library_entity_view_display_update()
layout_library_entity_view_display_insert in ./layout_library.module
Implements hook_ENTITY_TYPE_insert().

File

./layout_library.module, line 23
Provides hook implementations for Layout Library.

Code

function layout_library_entity_view_display_update(LayoutEntityDisplayInterface $display) {

  // This function changes config, so bail out immediately during config sync.
  if (\Drupal::isConfigSyncing()) {
    return;
  }
  if (!$display
    ->isLayoutBuilderEnabled()) {
    return;
  }
  $entity_type = $display
    ->getTargetEntityTypeId();
  $bundle = $display
    ->getTargetBundle();
  $field_name = 'layout_selection';

  // If the display supports layout overrides, create an entity reference field
  // so that entities which are using layout can "import" a stored layout from
  // their library as a base for customization. If the display does not support
  // overrides, remove the field from the target entity type and bundle, if it
  // exists.
  if ($display
    ->getThirdPartySetting('layout_library', 'enable', FALSE)) {

    // Reuse the existing field storage if possible.
    $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
    if (!$field_storage) {
      $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => $entity_type,
        'type' => 'entity_reference',
      ]);
      $field_storage
        ->setSetting('target_type', 'layout');
      $field_storage
        ->setLocked(TRUE);
      $field_storage
        ->save();
    }

    // Create the entity reference field if it doesn't already exist.
    $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
    if (!$field) {
      $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => $bundle,
      ]);
      $field
        ->setSetting('handler', 'layout_library');
      $field
        ->setLabel(t('Layout'));
      $field
        ->save();
    }
    $form_display = \Drupal::service('entity_display.repository')
      ->getFormDisplay($entity_type, $bundle, 'default');
    if (!$form_display
      ->getComponent($field_name)) {
      $form_display
        ->setComponent($field_name, [
        'type' => \Drupal::moduleHandler()
          ->moduleExists('options') ? 'options_select' : 'entity_reference_autocomplete',
        'region' => 'content',
      ])
        ->save();
    }
  }
  else {
    $field_is_used = (bool) \Drupal::entityTypeManager()
      ->getStorage($display
      ->getEntityTypeId())
      ->getQuery()
      ->condition('targetEntityType', $display
      ->getTargetEntityTypeId())
      ->condition('bundle', $display
      ->getTargetBundle())
      ->condition('mode', $display
      ->getMode(), '<>')
      ->condition('third_party_settings.layout_library.enable', TRUE)
      ->count()
      ->execute();
    if (!$field_is_used && ($field = FieldConfig::loadByName($entity_type, $bundle, $field_name))) {
      $field
        ->delete();
      field_purge_batch(10);
      \Drupal::service('entity_display.repository')
        ->getFormDisplay($entity_type, $bundle, 'default')
        ->removeComponent($field_name)
        ->save();
    }
  }
}