You are here

protected function JsonapiHelper::addOptionFromJson in Entity Share 8.2

Same name and namespace in other branches
  1. 8 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper::addOptionFromJson()

Helper function to add an option.

Parameters

array $options: The array of options for the tableselect form type element.

array $data: An array of data.

\Drupal\entity_share_client\Entity\RemoteInterface $remote: The selected remote.

string $channel_id: The selected channel id.

int $level: The level of indentation.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\InvalidArgumentException

1 call to JsonapiHelper::addOptionFromJson()
JsonapiHelper::buildEntitiesOptions in modules/entity_share_client/src/Service/JsonapiHelper.php
Prepare entities from an URI to request.

File

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

Class

JsonapiHelper
Class JsonapiHelper.

Namespace

Drupal\entity_share_client\Service

Code

protected function addOptionFromJson(array &$options, array $data, RemoteInterface $remote, $channel_id, $level = 0) {
  $parsed_type = explode('--', $data['type']);
  $entity_type_id = $parsed_type[0];
  $bundle_id = $parsed_type[1];
  $entity_type = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->getEntityType();
  $entity_keys = $entity_type
    ->getKeys();
  $resource_type = $this->resourceTypeRepository
    ->get($entity_type_id, $bundle_id);
  $status_info = $this->stateInformation
    ->getStatusInfo($data);

  // Prepare remote changed info.
  $remote_changed_info = '';
  if ($resource_type
    ->hasField('changed')) {
    $changed_public_name = $resource_type
      ->getPublicName('changed');
    if (!empty($data['attributes'][$changed_public_name])) {
      if (is_numeric($data['attributes'][$changed_public_name])) {
        $remote_changed_date = DrupalDateTime::createFromTimestamp($data['attributes'][$changed_public_name]);
        $remote_changed_info = $remote_changed_date
          ->format(self::CHANGED_FORMAT, [
          'timezone' => date_default_timezone_get(),
        ]);
      }
      elseif ($remote_changed_date = DrupalDateTime::createFromFormat(\DateTime::RFC3339, $data['attributes'][$changed_public_name])) {
        $remote_changed_info = $remote_changed_date
          ->format(self::CHANGED_FORMAT, [
          'timezone' => date_default_timezone_get(),
        ]);
      }
    }
  }
  $options[$data['id']] = [
    'label' => $this
      ->getOptionLabel($data, $status_info, $entity_keys, $remote
      ->get('url'), $level),
    'type' => $entity_type
      ->getLabel(),
    'bundle' => $this->bundleInfos[$entity_type_id][$bundle_id]['label'],
    'language' => $this
      ->getEntityLanguageLabel($data, $entity_keys),
    'changed' => $remote_changed_info,
    'status' => [
      'data' => $status_info['label'],
      'class' => $status_info['class'],
    ],
  ];
  $id_public_name = $resource_type
    ->getPublicName($entity_keys['id']);
  if ($this->moduleHandler
    ->moduleExists('diff') && in_array($status_info['info_id'], [
    StateInformationInterface::INFO_ID_CHANGED,
    StateInformationInterface::INFO_ID_NEW_TRANSLATION,
  ]) && !is_null($status_info['local_revision_id']) && isset($data['attributes'][$id_public_name])) {
    $options[$data['id']]['status']['data'] = new FormattableMarkup('@label: @diff_link', [
      '@label' => $options[$data['id']]['status']['data'],
      '@diff_link' => Link::createFromRoute($this
        ->t('Diff'), 'entity_share_client.diff', [
        'left_revision' => $status_info['local_revision_id'],
        'remote' => $remote
          ->id(),
        'channel_id' => $channel_id,
        'uuid' => $data['id'],
      ], [
        'attributes' => [
          'class' => [
            'use-ajax',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'width' => '90%',
          ]),
        ],
      ])
        ->toString(),
    ]);
  }
}