You are here

layout_builder_st.module in Layout Builder Symmetric Translations 8

File

layout_builder_st.module
View source
<?php

use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\layout_builder_st\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder_st\Form\BlockContentInlineBlockTranslateForm;
use Drupal\layout_builder\Form\OverridesEntityForm as CoreOverridesEntityForm;
use Drupal\layout_builder_st\Form\BlockPluginTranslationForm;
use Drupal\layout_builder_st\Form\OverridesEntityForm;
use Drupal\layout_builder_st\Plugin\Block\InlineBlock;
use Drupal\layout_builder_st\Plugin\Field\FieldWidget\LayoutBuilderWidget;
use Drupal\layout_builder_st\Plugin\SectionStorage\OverridesSectionStorage;

/**
 * Implements hook_module_implements_alter().
 */
function layout_builder_st_module_implements_alter(&$implementations, $hook) {
  switch ($hook) {

    // Move our hook_entity_type_alter() implementation to the end of the list.
    // it must run after layout_builder_st_entity_type_alter().
    case 'entity_type_alter':
      $group = $implementations['layout_builder_st'];
      unset($implementations['layout_builder_st']);
      $implementations['layout_builder_st'] = $group;
      break;
  }
}

/**
 * Implements hook_entity_type_alter().
 *
 * Copied from https://www.drupal.org/project/drupal/issues/2946333#comment-13129737
 */
function layout_builder_st_entity_type_alter(array &$entity_types) {

  // Replace entity_view_display class with our own.

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types['entity_view_display']
    ->setClass(LayoutBuilderEntityViewDisplay::class);

  /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
  foreach ($entity_types as $entity_type) {
    if ($entity_type
      ->entityClassImplements(FieldableEntityInterface::class)) {
      $form_class = $entity_type
        ->getFormClass('layout_builder');
      if ($form_class !== CoreOverridesEntityForm::class) {
        \Drupal::messenger()
          ->addError(t('The "layout_builder" form class for @entity_type entity type is already overriden by @class. This is incompatible with the layout_builder_st module'), [
          '@entity_type' => $entity_type
            ->getLabel(),
          '@class' => $form_class,
        ]);
      }
      $entity_type
        ->setFormClass('layout_builder', OverridesEntityForm::class);
    }
  }
  if (isset($entity_types['block_content'])) {
    $entity_types['block_content']
      ->setFormClass('layout_builder_translate', BlockContentInlineBlockTranslateForm::class);
  }
}

/**
 * Implements hook_block_alter().
 */
function layout_builder_st_block_alter(&$definitions) {
  foreach ($definitions as &$definition) {
    if ($definition['id'] === 'inline_block') {

      // Replace with our extended InlineBlock class to handle translations.
      $definition['class'] = InlineBlock::class;
    }
  }
}

/**
 * Implements hook_layout_builder_section_storage_alter().
 *
 * @param \Drupal\layout_builder\SectionStorage\SectionStorageDefinition[] $definitions
 */
function layout_builder_st_layout_builder_section_storage_alter(array &$definitions) {
  $definition = $definitions['overrides'];
  $definition
    ->setClass(OverridesSectionStorage::class);
}

/**
 * Implements hook_field_widget_info_alter().
 */
function layout_builder_st_field_widget_info_alter(array &$info) {
  if (isset($info['layout_builder_widget'])) {
    $info['layout_builder_widget']['field_types'][] = 'layout_translation';
    $info['layout_builder_widget']['class'] = LayoutBuilderWidget::class;
    $info['layout_builder_widget']['provider'] = 'layout_builder_st';
  }
}

/**
 * Implements hook_modules_installed().
 */
function layout_builder_st_modules_installed($modules) {
  if (in_array('layout_builder_at', $modules)) {
    \Drupal::messenger()
      ->addError('Layout Builder Symmetric Translations is not compatible with Layout Builder Asymmetric Translations. One of these should be uninstalled');
  }
}