You are here

function name_get_additional_component in Name Field 8

Helper function to an additional component for an item.

Parameters

\Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager: The entity type manager to generate the view.

\Drupal\Core\Render\RendererInterface $renderer: The renderer to render the value.

\Drupal\Core\Field\FieldItemListInterface $items: The items to render.

string $key_value: The key value.

string $sep_value: The separator value to use when handling multiple values.

Return value

string The value of the additional component.

2 calls to name_get_additional_component()
NameFormatter::parseAdditionalComponents in src/Plugin/Field/FieldFormatter/NameFormatter.php
Gets any additional linked components.
name_user_load in ./name.module
Implements hook_user_load().

File

./name.module, line 210
Defines an API for displaying and inputing names.

Code

function name_get_additional_component(EntityTypeManagerInterface $entityTypeManager, RendererInterface $renderer, FieldItemListInterface $items, $key_value, $sep_value) {
  if ($key_value) {
    $parent = $items
      ->getEntity();
    if ($key_value == '_self') {
      if ($label = $parent
        ->label()) {
        return $label;
      }
    }
    elseif (strpos($key_value, '_self_property') === 0) {
      $property = str_replace('_self_property_', '', $key_value);
      try {
        if ($item = $parent
          ->get($property)) {
          if (!empty($item->value)) {
            return $item->value;
          }
        }
      } catch (\InvalidArgumentException $e) {
      }
    }
    elseif ($parent
      ->hasField($key_value)) {
      $target_items = $parent
        ->get($key_value);
      if (!$target_items
        ->isEmpty() && $target_items
        ->access('view')) {
        $field = $target_items
          ->getFieldDefinition();
        $values = [];
        switch ($field
          ->getType()) {
          case 'entity_reference':
            foreach ($target_items as $item) {
              if (!empty($item->entity) && $item->entity
                ->access('view') && ($label = $item->entity
                ->label())) {
                $values[] = $label;
              }
            }
            break;
          default:
            $viewBuilder = $entityTypeManager
              ->getViewBuilder($parent
              ->getEntityTypeId());
            foreach ($target_items as $item) {
              try {
                $renderable = $viewBuilder
                  ->viewFieldItem($item, [
                  'label' => 'hidden',
                ]);

                /* @var $value \Drupal\Component\Render\MarkupInterface */
                if ($value = (string) $renderer
                  ->render($renderable)) {

                  // Remove any markup, but decode entities as the parser
                  // requires raw unescaped strings.
                  if ($value = trim(strip_tags($value))) {
                    $values[] = HTML::decodeEntities($value);
                  }
                }
              } catch (\Exception $e) {
              }
            }
            break;
        }
        if ($values) {
          $sep_value = HTML::decodeEntities(trim(strip_tags($sep_value)));
          return implode($sep_value, $values);
        }
      }
    }
  }
  return '';
}