You are here

layout_builder_st.install in Layout Builder Symmetric Translations 8

File

layout_builder_st.install
View source
<?php

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\layout_builder_st\Plugin\SectionStorage\OverridesSectionStorage;

/**
 * Implements hook_install().
 */
function layout_builder_st_install() {

  // Add the layout translation settings field.

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
  $field_manager = \Drupal::service('entity_field.manager');
  $field_map = $field_manager
    ->getFieldMap();
  foreach ($field_map as $entity_type_id => $field_infos) {
    if (isset($field_infos[\Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::FIELD_NAME]['bundles'])) {
      $non_translatable_bundle_count = 0;
      foreach ($field_infos[OverridesSectionStorage::FIELD_NAME]['bundles'] as $bundle) {
        $bundles_not_added = [];

        // The field map can contain stale information. If the field does not
        // exist, ignore it. The field map will be rebuilt when the cache is
        // cleared at the end of the update process.
        if (!($field_config = FieldConfig::loadByName($entity_type_id, $bundle, OverridesSectionStorage::FIELD_NAME))) {
          continue;
        }
        _layout_builder_st_add_translation_field($entity_type_id, $bundle);
      }
    }
  }
}

/**
 * Adds a layout translation field to a given bundle.
 *
 * @param string $entity_type_id
 *   The entity type ID.
 * @param string $bundle
 *   The bundle.
 */
function _layout_builder_st_add_translation_field($entity_type_id, $bundle) {
  $field_name = OverridesSectionStorage::TRANSLATED_CONFIGURATION_FIELD_NAME;
  $field = FieldConfig::loadByName($entity_type_id, $bundle, $field_name);
  if (!$field) {
    $field_storage = FieldStorageConfig::loadByName($entity_type_id, $field_name);
    if (!$field_storage) {
      $field_storage = FieldStorageConfig::create([
        'entity_type' => $entity_type_id,
        'field_name' => $field_name,
        'type' => 'layout_translation',
        'locked' => TRUE,
      ]);
      $field_storage
        ->setTranslatable(TRUE);
      $field_storage
        ->save();
    }
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $bundle,
      'label' => t('Layout Labels'),
    ]);
    $field
      ->save();
  }
}

/**
 * Implements hook_requirements().
 */
function layout_builder_st_requirements($phase) {
  $requirements = [];
  if ($phase === 'install' && \Drupal::moduleHandler()
    ->moduleExists('layout_builder_at')) {
    $requirements['layout_builder_at_incompatibility'] = [
      'severity' => REQUIREMENT_ERROR,
      'description' => t('Layout Builder Symmetric Translations can not be installed when Layout Builder Asymmetric Translations is also installed.'),
    ];
  }
  if ($phase === 'runtime' && \Drupal::moduleHandler()
    ->moduleExists('layout_builder_at')) {
    $requirements['layout_builder_at_incompatibility'] = [
      'severity' => REQUIREMENT_ERROR,
      'description' => t('Layout Builder Symmetric Translations is not compatible with Layout Builder Asymmetric Translations. One of these should be uninstalled'),
    ];
  }
  return $requirements;
}

Functions

Namesort descending Description
layout_builder_st_install Implements hook_install().
layout_builder_st_requirements Implements hook_requirements().
_layout_builder_st_add_translation_field Adds a layout translation field to a given bundle.