You are here

public function LayoutBuilderCopyWidget::extractFormValues in Layout Builder Asymmetric Translation 8.2

Extract form values.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items:

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

Throws

\Drupal\Core\Entity\EntityStorageException

\Drupal\Core\TypedData\Exception\ReadOnlyException

Overrides WidgetBase::extractFormValues

File

src/Plugin/Field/FieldWidget/LayoutBuilderCopyWidget.php, line 191

Class

LayoutBuilderCopyWidget
A widget to display the copy widget form.

Namespace

Drupal\layout_builder_at\Plugin\Field\FieldWidget

Code

public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {

  // @todo This isn't resilient to being set twice, during validation and
  //   save https://www.drupal.org/project/drupal/issues/2833682.
  if (!$form_state
    ->isValidationComplete()) {
    return;
  }
  $field_name = $this->fieldDefinition
    ->getName();

  // We can only copy if the field is set and access is TRUE.
  if (isset($form[$field_name]['widget']['#layout_builder_at_access']) && !$form[$field_name]['widget']['#layout_builder_at_access']) {
    return;
  }

  // Extract the values from $form_state->getValues().
  $path = array_merge($form['#parents'], [
    $field_name,
  ]);
  $key_exists = NULL;
  $values = NestedArray::getValue($form_state
    ->getValues(), $path, $key_exists);
  $values = $this
    ->massageFormValues($values, $form, $form_state);
  if (isset($values['value']) && $values['value']) {

    // Replicate.

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */

    /** @var \Drupal\Core\Entity\ContentEntityInterface $default_entity */
    $entity = $items
      ->getEntity();
    $sourceLanguage = NULL;
    if ($form_state
      ->hasValue('source_langcode')) {
      $sourceLanguageArray = $form_state
        ->getValue('source_langcode');
      if (isset($sourceLanguageArray['source'])) {
        $sourceLanguage = $sourceLanguageArray['source'];
      }
    }
    $default_entity = is_null($sourceLanguage) ? $entity
      ->getUntranslated() : $entity
      ->getTranslation($sourceLanguage);

    /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $layout */
    $layout = $default_entity
      ->get(OverridesSectionStorage::FIELD_NAME);
    $uuid = \Drupal::service('uuid');

    /** @var \Drupal\layout_builder\Section[] $sections */
    $sections = $layout
      ->getSections();
    $new_sections = [];
    foreach ($sections as $delta => $section) {
      $cloned_section = clone $section;

      // Remove components from the cloned section.
      foreach ($cloned_section
        ->getComponents() as $c) {
        $cloned_section
          ->removeComponent($c
          ->getUuid());
      }

      // Sort the components by weight.
      $components = $section
        ->getComponents();
      uasort($components, function (SectionComponent $a, SectionComponent $b) {
        return $a
          ->getWeight() > $b
          ->getWeight() ? 1 : -1;
      });
      foreach ($components as $component) {
        $add_component = TRUE;
        $cloned_component = clone $component;
        $configuration = $component
          ->get('configuration');

        // Replicate inline block content.
        if ($this
          ->isInlineBlock($configuration['id'])) {

          /** @var \Drupal\block_content\BlockContentInterface $block */

          /** @var \Drupal\block_content\BlockContentInterface $replicated_block */
          $block = \Drupal::service('entity_type.manager')
            ->getStorage('block_content')
            ->loadRevision($configuration['block_revision_id']);
          $replicated_block = $this
            ->cloneEntity('block_content', $block
            ->id());
          if ($replicated_block) {
            $replicated_block
              ->set('langcode', $entity
              ->language()
              ->getId());
            $replicated_block
              ->save();
            $configuration = $this
              ->updateComponentConfiguration($configuration, $replicated_block);
            $cloned_component
              ->setConfiguration($configuration);

            // Store usage.
            \Drupal::service('inline_block.usage')
              ->addUsage($replicated_block
              ->id(), $entity);
          }
          else {
            $add_component = FALSE;
            $this
              ->messenger()
              ->addMessage($this
              ->t('The inline block "@label" was not duplicated.', [
              '@label' => $block
                ->label(),
            ]));
          }
        }

        // Add component.
        if ($add_component) {
          $cloned_component
            ->set('uuid', $uuid
            ->generate());
          $cloned_section
            ->appendComponent($cloned_component);
        }
      }
      $new_sections[] = $cloned_section;
    }
    $items
      ->setValue($new_sections);
  }
  else {
    $items
      ->setValue(NULL);
  }
}