You are here

protected function JsonapiHelper::getOptionLabel in Entity Share 8.2

Helper function to calculate the label to display in the table.

Parameters

array $data: An array of data.

array $status_info: An array of status info as returned by StateInformationInterface::getStatusInfo().

array $entity_keys: The entity keys.

string $remote_url: The remote url.

int $level: The level of indentation.

Return value

\Drupal\Component\Render\FormattableMarkup|string The prepared label.

1 call to JsonapiHelper::getOptionLabel()
JsonapiHelper::addOptionFromJson in modules/entity_share_client/src/Service/JsonapiHelper.php
Helper function to add an option.

File

modules/entity_share_client/src/Service/JsonapiHelper.php, line 667

Class

JsonapiHelper
Class JsonapiHelper.

Namespace

Drupal\entity_share_client\Service

Code

protected function getOptionLabel(array $data, array $status_info, array $entity_keys, $remote_url, $level) {
  $indentation = '';
  for ($i = 1; $i <= $level; $i++) {
    $indentation .= '<div class="indentation">&nbsp;</div>';
  }
  $parsed_type = explode('--', $data['type']);
  $entity_type_id = $parsed_type[0];
  $bundle_id = $parsed_type[1];
  $resource_type = $this->resourceTypeRepository
    ->get($entity_type_id, $bundle_id);
  $label_public_name = FALSE;
  if (isset($entity_keys['label']) && $resource_type
    ->hasField($entity_keys['label'])) {
    $label_public_name = $resource_type
      ->getPublicName($entity_keys['label']);
  }

  // Some entity type may not have a label key and the label is calculated
  // using the label() method on the entity but at this step the entity is not
  // denormalized and also as we are not on the server website, we would not
  // have the data required to calculate the entity's label.
  if (isset($data['attributes'][$label_public_name])) {
    $label = $data['attributes'][$label_public_name];
  }
  elseif (isset($entity_keys['id']) && $resource_type
    ->hasField($entity_keys['id'])) {
    $label = $data['attributes'][$resource_type
      ->getPublicName($entity_keys['id'])];
  }
  else {
    $label = $data['id'];
  }

  // Get link to remote entity. Need to manually create the link to avoid
  // getting alias from local website.
  if (isset($entity_keys['id']) && $resource_type
    ->hasField($entity_keys['id'])) {
    $remote_entity_id = $data['attributes'][$resource_type
      ->getPublicName($entity_keys['id'])];
    $entity_definition = $this->entityDefinitions[$entity_type_id];
    if ($entity_definition
      ->hasLinkTemplate('canonical')) {
      $canonical_path = $entity_definition
        ->getLinkTemplate('canonical');
      $remote_entity_path = str_replace('{' . $entity_type_id . '}', $remote_entity_id, $canonical_path);
      $remote_entity_url = Url::fromUri($remote_url . $remote_entity_path);
      $label = Link::fromTextAndUrl($label, $remote_entity_url)
        ->toString();
    }
  }

  // Prepare link to local entity if it exists.
  $local_link = '';
  if (!is_null($status_info['local_entity_link'])) {
    $local_link = new Link($this
      ->t('(View local)'), $status_info['local_entity_link']);
    $local_link = $local_link
      ->toString();
  }
  $label = new FormattableMarkup($indentation . '@label ' . $local_link, [
    '@label' => $label,
  ]);
  return $label;
}