You are here

public function PatternEditForm::buildEntity in Pathauto 8

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 214

Class

PatternEditForm
Edit form for pathauto patterns.

Namespace

Drupal\pathauto\Form

Code

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

  /** @var \Drupal\pathauto\PathautoPatternInterface $entity */
  $entity = parent::buildEntity($form, $form_state);

  // Will only be used for new patterns.
  $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'));
    }
  }
  if ($entity
    ->isNew()) {
    $entity
      ->setWeight($default_weight);
  }
  return $entity;
}