You are here

public static function EntityconnectController::addInfo in Entity connect 8.2

Callback for creating the build array of entity types to add.

Parameters

string $cache_id: The parent form cache id.

string $entity_type: The target entity type.

array $acceptable_types: An array of types that can be added via entityconnect.

Return value

array The build array of entity types to add

Throws

\Exception

1 call to EntityconnectController::addInfo()
entityconnect_entityconnect_add_info in ./entityconnect.module
Implements hook_entityconnect_add_info().

File

src/Controller/EntityconnectController.php, line 309

Class

EntityconnectController
Returns responses for Entityconnect module routes.

Namespace

Drupal\entityconnect\Controller

Code

public static function addInfo($cache_id, $entity_type, array $acceptable_types) {
  if (empty($entity_type)) {
    throw new \Exception('Entity type can not be empty');
  }
  $content = [];
  $routes = static::getAddRoute($entity_type);
  $options = [
    'query' => [
      'build_cache_id' => $cache_id,
      'child' => TRUE,
    ],
    'absolute' => TRUE,
  ];
  if (!empty($routes)) {
    $route_name = key($routes);

    /** @var \Symfony\Component\Routing\Route $route */
    $route = current($routes);

    // If no parameters just try to get the url from route name.
    if (empty($params = $route
      ->getOption('parameters'))) {
      $content[$entity_type]['href'] = \Drupal::urlGenerator()
        ->generateFromRoute($route_name, [], $options);
    }
    else {

      // Should only be one parameter.
      $route_param_key = key($params);
      foreach ($acceptable_types as $acceptable_type) {
        $type = \Drupal::entityTypeManager()
          ->getStorage($route_param_key)
          ->load($acceptable_type);
        if ($type) {
          $route_params = [
            $route_param_key => $acceptable_type,
          ];
          $href = \Drupal::urlGenerator()
            ->generateFromRoute($route_name, $route_params, $options);
          $content[$type
            ->id()] = [
            'href' => $href,
            'label' => $type
              ->label(),
            'description' => method_exists($type, 'getDescription') ? $type
              ->getDescription() : '',
          ];
        }
      }
    }
  }
  if (!empty($content)) {
    return [
      'content' => $content,
    ];
  }
  return [];
}