You are here

protected function EntityShareBlockFieldEnhancer::doUndoTransform in Entity Share 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/jsonapi/FieldEnhancer/EntityShareBlockFieldEnhancer.php \Drupal\entity_share\Plugin\jsonapi\FieldEnhancer\EntityShareBlockFieldEnhancer::doUndoTransform()

File

src/Plugin/jsonapi/FieldEnhancer/EntityShareBlockFieldEnhancer.php, line 72

Class

EntityShareBlockFieldEnhancer
Prepare block field value to be able to handle block content entities.

Namespace

Drupal\entity_share\Plugin\jsonapi\FieldEnhancer

Code

protected function doUndoTransform($data, Context $context) {
  if (isset($data['settings']['id'])) {
    $parsed_id = [];

    // Check if it is a link to an entity.
    preg_match("/block_content:(.*)/", $data['settings']['id'], $parsed_id);
    if (!empty($parsed_id)) {
      $block_content_uuid = $parsed_id[1];

      /** @var \Drupal\block_content\BlockContentInterface[] $block_content */
      $block_contents = $this->entityTypeManager
        ->getStorage('block_content')
        ->loadByProperties([
        'uuid' => $block_content_uuid,
      ]);
      if (!empty($block_contents)) {
        $block_content = array_shift($block_contents);
        $route_name = sprintf('jsonapi.%s--%s.individual', 'block_content', $block_content
          ->bundle());
        $url = Url::fromRoute($route_name, [
          'entity' => $block_content_uuid,
        ])
          ->setOption('language', $this->languageManager
          ->getCurrentLanguage())
          ->setOption('absolute', TRUE);
        $data['block_content_href'] = $url
          ->toString();
      }
    }
  }
  return $data;
}