You are here

public function PatternEditForm::buildEntity in View Mode Page 8.3

Same name and namespace in other branches
  1. 4.0.x src/Form/PatternEditForm.php \Drupal\view_mode_page\Form\PatternEditForm::buildEntity()
  2. 3.2.x src/Form/PatternEditForm.php \Drupal\view_mode_page\Form\PatternEditForm::buildEntity()

Builds an updated entity object based upon the submitted form values.

For building the updated entity object the form's entity is cloned and the submitted form values are copied to entity properties. The form's entity remains unchanged.

Parameters

array $form: A nested array form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

\Drupal\Core\Entity\EntityInterface An updated copy of the form's entity object.

Overrides EntityForm::buildEntity

See also

\Drupal\Core\Entity\EntityFormInterface::getEntity()

1 call to PatternEditForm::buildEntity()
PatternEditForm::submitSelectType in src/Form/PatternEditForm.php
Handles submit call when alias type is selected.

File

src/Form/PatternEditForm.php, line 229

Class

PatternEditForm
Edit form for view_mode_page patterns.

Namespace

Drupal\view_mode_page\Form

Code

public function buildEntity(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\view_mode_page\ViewmodepagePatternInterface $entity */
  $entity = parent::buildEntity($form, $form_state);
  $default_weight = 0;
  $alias_type = $entity
    ->getAliasType();
  if ($alias_type
    ->getDerivativeId() && $this->entityTypeManager
    ->hasDefinition($alias_type
    ->getDerivativeId())) {
    $entity_type = $alias_type
      ->getDerivativeId();

    // First, remove bundle and language conditions.
    foreach ($entity
      ->getSelectionConditions() as $condition_id => $condition) {
      if (in_array($condition
        ->getPluginId(), [
        'entity_bundle:' . $entity_type,
        'node_type',
        'language',
      ])) {
        $entity
          ->removeSelectionCondition($condition_id);
      }
    }
    if ($bundles = array_filter((array) $form_state
      ->getValue('bundles'))) {
      $default_weight -= 5;
      $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type;
      $entity
        ->addSelectionCondition([
        'id' => $plugin_id,
        'bundles' => $bundles,
        'negate' => FALSE,
        'context_mapping' => [
          $entity_type => $entity_type,
        ],
      ]);
    }
    if ($languages = array_filter((array) $form_state
      ->getValue('languages'))) {
      $default_weight -= 5;
      $language_mapping = $entity_type . ':' . $this->entityTypeManager
        ->getDefinition($entity_type)
        ->getKey('langcode') . ':language';
      $entity
        ->addSelectionCondition([
        'id' => 'language',
        'langcodes' => array_combine($languages, $languages),
        'negate' => FALSE,
        'context_mapping' => [
          'language' => $language_mapping,
        ],
      ]);
      $entity
        ->addRelationship($language_mapping, $this
        ->t('Language'));
    }
  }
  $entity
    ->setWeight($default_weight);
  return $entity;
}