You are here

public function ContentEntityConflictHandler::entityFormAlter in Conflict 8.2

Performs the needed alterations to the entity form.

Parameters

array $form: The entity form to be altered to provide the translation workflow.

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

\Drupal\Core\Entity\EntityInterface $entity: The entity being created or edited.

bool $inline_entity_form: (optional) TRUE if an inline entity form is given, FALSE if a regular entity form is given. Defaults to FALSE.

Overrides EntityConflictHandlerInterface::entityFormAlter

File

src/Entity/ContentEntityConflictHandler.php, line 126

Class

ContentEntityConflictHandler

Namespace

Drupal\conflict\Entity

Code

public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity, $inline_entity_form = FALSE) {

  // Let the conflict module updates the other translations before any other
  // entity builder has run, otherwise we might overwrite changes that will be
  // made by the entity builders on other translations. An example for this is
  // \Drupal\content_translation\ContentTranslationHandler::entityFormEntityBuild().
  $form['#entity_builders'] = isset($form['#entity_builders']) ? $form['#entity_builders'] : [];
  array_unshift($form['#entity_builders'], [
    $this,
    'entityFormEntityBuilder',
  ]);
  if (!isset($form['conflict_entity_original_hash'])) {
    $input = $form_state
      ->getUserInput();
    $hash_path = $form['#parents'];
    $hash_path[] = 'conflict_entity_original_hash';
    $hash = NestedArray::getValue($input, $hash_path) ?: $entity->{EntityConflictHandlerInterface::CONFLICT_ENTITY_ORIGINAL_HASH};
    $form['conflict_entity_original_hash'] = [
      '#type' => 'hidden',
      '#default_value' => $hash,
    ];
  }

  // @todo This check is actually not really needed, as #validate is only
  // executed at form level, not at inline form level, where only
  // #element_validate should be executed.
  if (!$inline_entity_form) {

    // @todo we have to ensure that our validate method is running at the end
    // but if there is another module which is moving its form_alter hook to
    // the end then there might be some collision. Should we decorate the
    // form builder instead and add our validate method after all the hooks
    // have run?
    $form['#validate'][] = [
      $this,
      'entityMainFormValidateLast',
    ];
  }
}