You are here

public function EntityconnectController::add in Entity connect 8.2

Add a new connecting entity.

Parameters

string $cache_id: The id of the parent form cache.

Return value

array|RedirectResponse The page of the entity to be added or a list of acceptable types.

1 string reference to 'EntityconnectController::add'
entityconnect.routing.yml in ./entityconnect.routing.yml
entityconnect.routing.yml

File

src/Controller/EntityconnectController.php, line 237

Class

EntityconnectController
Returns responses for Entityconnect module routes.

Namespace

Drupal\entityconnect\Controller

Code

public function add($cache_id) {
  $data = $this->entityconnectCache
    ->get($cache_id);
  $entity_type = $data['target_entity_type'];
  $acceptable_types = $data['acceptable_types'];
  $args = [
    $cache_id,
    $entity_type,
    $acceptable_types,
  ];
  $add_info = $this
    ->moduleHandler()
    ->invokeAll('entityconnect_add_info', $args);

  // Merge in default values.
  foreach ($add_info as $data) {
    $add_info += [
      'content' => [
        'href' => '',
        'label' => '',
        'description' => '',
      ],
      'theme_callback' => 'entityconnect_entity_add_list',
    ];
  }
  $context = [
    'cache_id' => $cache_id,
    'entity_type' => $entity_type,
    'acceptable_tpes' => $acceptable_types,
  ];
  $this
    ->moduleHandler()
    ->alter('entityconnect_add_info', $add_info, $context);
  if (isset($add_info)) {
    $content = $add_info['content'];
    $theme = $add_info['theme_callback'];
    if (count($content) == 1) {
      $item = array_pop($content);
      if (is_array($item['href'])) {
        $url = array_shift($item['href']);
      }
      else {
        $url = $item['href'];
      }
      if (!$url) {
        $this
          ->returnWithMessage($this
          ->t('Invalid url: %url', [
          '%url' => $url,
        ]), 'warning', $cache_id);
      }
      return new RedirectResponse($url);
    }
    return [
      '#theme' => $theme,
      '#items' => $content,
      '#cache_id' => $cache_id,
      '#cancel_link' => Link::createFromRoute($this
        ->t('Cancel'), 'entityconnect.return', [
        'cache_id' => $cache_id,
        'cancel' => TRUE,
      ]),
    ];
  }
  return $this
    ->returnWithMessage($this
    ->t('Nothing to add.'), 'warning', $cache_id);
}