You are here

protected static function ConflictResolutionDialogFormBuilder::getEntitiesForPropertyPath in Conflict 8.2

Returns the entity for the given property path.

Parameters

$path: The property path.

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

Return value

\Drupal\Core\Entity\EntityInterface[] The entities across the path.

Throws

\Exception An exception will be thrown in case the structure is not supported or it is not correct.

3 calls to ConflictResolutionDialogFormBuilder::getEntitiesForPropertyPath()
ConflictResolutionDialogFormBuilder::processForm in src/Form/ConflictResolutionDialogFormBuilder.php
Adds the conflict resolution overview to the form.
ConflictResolutionDialogFormBuilder::resolveConflictsAjax in src/Form/ConflictResolutionDialogFormBuilder.php
Ajax callback returning the UI for conflict resolution.
ConflictResolutionDialogFormBuilder::resolveConflictsSubmit in src/Form/ConflictResolutionDialogFormBuilder.php
Submit handler for starting the conflict resolution.

File

src/Form/ConflictResolutionDialogFormBuilder.php, line 376

Class

ConflictResolutionDialogFormBuilder

Namespace

Drupal\conflict\Form

Code

protected static function getEntitiesForPropertyPath($path, FormStateInterface $form_state) {
  $parents = $path ? explode('.', $path) : [];

  /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();
  $element = $form_object
    ->getEntity();
  $langcode = $element
    ->language()
    ->getId();
  $entities = [
    $element,
  ];
  while ($parents) {
    $next_element_identifier = array_shift($parents);
    if ($element instanceof ContentEntityInterface) {
      if (!$element
        ->hasField($next_element_identifier)) {
        throw new \Exception('Not supported structure.');
      }
      $element = $element
        ->get($next_element_identifier);
    }
    elseif ($element instanceof EntityReferenceFieldItemListInterface) {
      if (!isset($element[$next_element_identifier])) {
        throw new \Exception('Not supported structure.');
      }

      /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
      $field_item = $element
        ->get($next_element_identifier);
      $properties = $field_item
        ->getProperties(TRUE);
      if (!isset($properties['entity'])) {
        throw new \Exception('Not supported structure.');
      }

      /** @var \Drupal\Core\Entity\ContentEntityInterface $element */
      $element = $field_item->entity;
      if ($element
        ->hasTranslation($langcode)) {
        $element = $element
          ->getTranslation($langcode);
      }
      elseif (count($element
        ->getTranslationLanguages()) > 1) {
        throw new \Exception('Not supported structure.');
      }
      $entities[] = $element;
    }
    else {
      throw new \Exception('Not supported structure.');
    }
  }
  if (!$element instanceof EntityInterface) {
    throw new \Exception('Not supported structure.');
  }
  return $entities;
}