You are here

protected static function ConflictResolutionDialogFormBuilder::getTitleForPropertyPath in Conflict 8.2

Builds a title for the given property path.

Parameters

$path: The property path.

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

Return value

array The render array for building the markup for the given path.

Throws

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

File

src/Form/ConflictResolutionDialogFormBuilder.php, line 237

Class

ConflictResolutionDialogFormBuilder

Namespace

Drupal\conflict\Form

Code

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

  /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();
  $element = $form_object
    ->getEntity();
  $langcode = $element
    ->language()
    ->getId();
  $titles[] = $element
    ->getEntityType()
    ->getLabel() . ': "' . $element
    ->label() . '"';
  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.');
      }
      $titles[] = $element
        ->getEntityType()
        ->getLabel() . ': "' . $element
        ->label() . '"';
    }
    else {
      throw new \Exception('Not supported structure.');
    }
  }
  $title = [
    '#type' => 'nested_entity_list',
    '#titles' => $titles,
  ];
  return $title;
}