layout_builder_at.module in Layout Builder Asymmetric Translation 8.2
Same filename and directory in other branches
Provides hook implementations for Layout Builder Asymmetric Translation.
File
layout_builder_at.moduleView source
<?php
/**
* @file
* Provides hook implementations for Layout Builder Asymmetric Translation.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Site\Settings;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\field_ui\Form\EntityDisplayFormBase;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
/**
* Implements hook_module_implements_alter().
*/
function layout_builder_at_module_implements_alter(&$implementations, $hook) {
if (in_array($hook, [
'form_entity_form_display_edit_form_alter',
])) {
unset($implementations['layout_builder']);
}
}
/**
* Implements hook_theme_registry_alter().
*/
function layout_builder_at_theme_registry_alter(&$theme_registry) {
// Removes layout_builder_preprocess_language_content_settings_table() to
// remove the 'Non translatable' warning.
if (!empty($theme_registry['language_content_settings_table']['preprocess functions'])) {
$preprocess_functions =& $theme_registry['language_content_settings_table']['preprocess functions'];
$index = array_search('layout_builder_preprocess_language_content_settings_table', $preprocess_functions);
if ($index !== FALSE) {
unset($preprocess_functions[$index]);
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for \Drupal\field_ui\Form\EntityFormDisplayEditForm.
*/
function layout_builder_at_form_entity_form_display_edit_form_alter(&$form, FormStateInterface $form_state) {
$hide = TRUE;
$callback_object = $form_state
->getBuildInfo()['callback_object'];
if (isset($form['fields'][OverridesSectionStorage::FIELD_NAME]) && $callback_object instanceof EntityDisplayFormBase) {
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
$display = $callback_object
->getEntity();
$id = $display
->getTargetEntityTypeId() . '.' . $display
->getTargetBundle() . '.' . OverridesSectionStorage::FIELD_NAME;
$fieldConfig = FieldConfig::load($id);
if ($fieldConfig && $fieldConfig
->isTranslatable()) {
$hide = FALSE;
}
}
if ($hide) {
layout_builder_form_entity_form_display_edit_form_alter($form, $form_state);
}
else {
// Add an extra validation handler which prevents setting the layout widget
// to the original one from Layout Builder.
$form['#validate'][] = 'layout_builder_at_validate_form_display';
}
}
/**
* Validation callback for the manage form display screen.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function layout_builder_at_validate_form_display($form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
if (isset($values['fields'][OverridesSectionStorage::FIELD_NAME])) {
if ($values['fields'][OverridesSectionStorage::FIELD_NAME]['type'] == 'layout_builder_widget') {
$form_state
->setErrorByName('fields][' . OverridesSectionStorage::FIELD_NAME, t('You can not select the Layout Builder Widget, please select a different widget.'));
}
}
}
/**
* Implements hook_form_alter().
*/
function layout_builder_at_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form[OverridesSectionStorage::FIELD_NAME]) && isset($form[OverridesSectionStorage::FIELD_NAME]['widget']['#layout_builder_at_access']) && $form[OverridesSectionStorage::FIELD_NAME]['widget']['#layout_builder_at_access']) {
$form[OverridesSectionStorage::FIELD_NAME]['#access'] = TRUE;
$form[OverridesSectionStorage::FIELD_NAME]['widget']['value']['#title'] = t('Copy blocks into translation');
$form[OverridesSectionStorage::FIELD_NAME]['widget']['value']['#title_display'] = 'after';
}
}
/**
* Implements hook_layout_builder_section_storage_alter().
*/
function layout_builder_at_layout_builder_section_storage_alter(array &$definitions) {
/** @var \Drupal\layout_builder\SectionStorage\SectionStorageDefinition $overrides_storage_definition */
$overrides_storage_definition = $definitions['overrides'];
// Override the section storage definition to allow access to the layout tab
// on translations.
$overrides_storage_definition
->setClass('\\Drupal\\layout_builder_at\\Plugin\\SectionStorage\\TranslatableOverridesSectionStorage');
}
/**
* Implements hook_ENTITY_TYPE_presave() for 'field_storage_config'.
*/
function layout_builder_at_field_storage_config_presave(FieldStorageConfigInterface $field_storage) {
// Make the field translatable for asymmetric translations. Only act on
// layout builder section fields.
if ($field_storage
->getType() === 'layout_section' && $field_storage
->getName() === OverridesSectionStorage::FIELD_NAME) {
$field_storage
->setTranslatable(TRUE);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function layout_builder_at_form_layout_builder_add_block_alter(&$form, FormStateInterface $form_state) {
// Set the default language of the block based on the entity language.
if (isset($form['settings']['block_form']['#block']) && Settings::get('layout_builder_at_set_content_block_language_to_entity', TRUE)) {
/** @var \Drupal\block_content\Entity\BlockContent $block */
$block = $form['settings']['block_form']['#block'];
if ($block) {
$contexts = $form_state
->getTemporaryValue('gathered_contexts');
if ($contexts && $contexts['layout_builder.entity']) {
$entity = $contexts['layout_builder.entity']
->getContextValue('entity');
if ($entity) {
try {
$block
->set('langcode', $entity
->language()
->getId());
} catch (\Exception $ignored) {
}
}
}
}
}
}
Functions
Name | Description |
---|---|
layout_builder_at_field_storage_config_presave | Implements hook_ENTITY_TYPE_presave() for 'field_storage_config'. |
layout_builder_at_form_alter | Implements hook_form_alter(). |
layout_builder_at_form_entity_form_display_edit_form_alter | Implements hook_form_FORM_ID_alter() for \Drupal\field_ui\Form\EntityFormDisplayEditForm. |
layout_builder_at_form_layout_builder_add_block_alter | Implements hook_form_FORM_ID_alter(). |
layout_builder_at_layout_builder_section_storage_alter | Implements hook_layout_builder_section_storage_alter(). |
layout_builder_at_module_implements_alter | Implements hook_module_implements_alter(). |
layout_builder_at_theme_registry_alter | Implements hook_theme_registry_alter(). |
layout_builder_at_validate_form_display | Validation callback for the manage form display screen. |