You are here

function layout_builder_at_form_layout_builder_add_block_alter in Layout Builder Asymmetric Translation 8.2

Implements hook_form_FORM_ID_alter().

File

./layout_builder_at.module, line 116
Provides hook implementations for Layout Builder Asymmetric Translation.

Code

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) {
          }
        }
      }
    }
  }
}