class EntityconnectController in Entity connect 8.2
Returns responses for Entityconnect module routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\entityconnect\Controller\EntityconnectController implements ContainerInjectionInterface
Expanded class hierarchy of EntityconnectController
1 file declares its use of EntityconnectController
- entityconnect.module in ./
entityconnect.module - Contains entityconnect.module.
File
- src/
Controller/ EntityconnectController.php, line 19
Namespace
Drupal\entityconnect\ControllerView source
class EntityconnectController extends ControllerBase implements ContainerInjectionInterface {
/**
* Temporary session storage for entityconnect.
*
* @var \Drupal\entityconnect\EntityconnectCache
*/
protected $entityconnectCache;
/**
* Drupal renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Drupal\Core\Messenger\MessengerInterface definition.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new EntityconnectController.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* Renderer object.
* @param \Drupal\entityconnect\EntityconnectCache $entityconnectCache
* Entityconnect Cache object.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* Messenger object.
*/
public function __construct(RendererInterface $renderer, EntityconnectCache $entityconnectCache, MessengerInterface $messenger) {
$this->renderer = $renderer;
$this->entityconnectCache = $entityconnectCache;
$this->messenger = $messenger;
}
/**
* Uses Symfony's ContainerInterface to declare dependency for constructor.
*
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('entityconnect.cache'), $container
->get('messenger'));
}
/**
* We redirect to the form page with the build_cache_id as a get param.
*
* @param string $cache_id
* Build cache id.
* @param bool $cancel
* Whether or not the request was cancelled.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* The url of the parent page.
*/
public function returnTo($cache_id, $cancel = FALSE) {
$cache_data = $this->entityconnectCache
->get($cache_id);
$cache_data['cancel'] = $cancel;
$this->entityconnectCache
->set($cache_id, $cache_data);
$css_id = 'edit-' . str_replace('_', '-', $cache_data['field']) . '-wrapper';
$options = [
'query' => [
'build_cache_id' => $cache_id,
'return' => TRUE,
],
'fragment' => $css_id,
];
// Collect additional request parameters, skip 'q', since this is
// the destination.
foreach ($cache_data['params'] as $key => $value) {
if ('build_cache_id' == $key) {
continue;
}
$options['query'][$key] = $value;
}
$options['absolute'] = TRUE;
$url = Url::fromRoute($cache_data['dest_route_name'], $cache_data['dest_route_params'], $options);
return new RedirectResponse($url
->toString());
}
/**
* Page callback: Redirect to edit form.
*
* @param string $cache_id
* The id of the parent form cache.
*
* @return array|RedirectResponse
* The page of the entity to edit or list of entities.
*/
public function edit($cache_id) {
$data = $this->entityconnectCache
->get($cache_id);
$entity_type = $data['target_entity_type'];
$target_id = $this
->fixTargetId($data['target_id']);
$args = [
$cache_id,
$entity_type,
$target_id,
];
$edit_info = $this
->moduleHandler()
->invokeAll('entityconnect_edit_info', $args);
// Merge in default values.
foreach ($edit_info as $data) {
$edit_info += [
'content' => [
'href' => '',
'label' => '',
'description' => '',
],
'theme_callback' => 'entityconnect_entity_add_list',
];
}
$context = [
'cache_id' => $cache_id,
'entity_type' => $entity_type,
'target_id' => $target_id,
];
$this
->moduleHandler()
->alter('entityconnect_edit_info', $edit_info, $context);
if (isset($edit_info)) {
$content = $edit_info['content'];
$theme = $edit_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 edit.'), 'warning', $cache_id);
}
/**
* Callback for creating the build array of entities to edit.
*
* @param string $cache_id
* The id of parent form cache.
* @param string $entity_type
* The target entity type.
* @param array|int $target_id
* The target id.
*
* @return array
* The edit build array.
*
* @throws \Exception
*/
public static function editInfo($cache_id, $entity_type, $target_id) {
if (empty($entity_type)) {
throw new \Exception('Entity type can not be empty');
}
if (empty($target_id)) {
throw new \Exception('Target_id can not be empty');
}
$content = [];
$options = [
'query' => [
'build_cache_id' => $cache_id,
'child' => TRUE,
],
'absolute' => TRUE,
];
if (is_array($target_id)) {
$info = \Drupal::entityTypeManager()
->getStorage($entity_type)
->loadMultiple($target_id);
foreach ($info as $key => $value) {
$content[$key] = [
'label' => $value
->label(),
'href' => \Drupal::urlGenerator()
->generateFromRoute('entity.' . $entity_type . '.edit_form', [
$entity_type => $key,
], $options),
'description' => '',
];
}
}
else {
$content[$entity_type]['href'] = \Drupal::urlGenerator()
->generateFromRoute('entity.' . $entity_type . '.edit_form', [
$entity_type => $target_id,
], $options);
}
return [
'content' => $content,
];
}
/**
* Add a new connecting entity.
*
* @param string $cache_id
* The id of the parent form cache.
*
* @return array|RedirectResponse
* The page of the entity to be added or a list of acceptable types.
*/
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);
}
/**
* Callback for creating the build array of entity types to add.
*
* @param string $cache_id
* The parent form cache id.
* @param string $entity_type
* The target entity type.
* @param array $acceptable_types
* An array of types that can be added via entityconnect.
*
* @return array
* The build array of entity types to add
*
* @throws \Exception
*/
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 [];
}
/**
* Sets a message upon return to help with errors.
*
* @param string $msg
* The message to display.
* @param string $status
* Message status.
* @param string $cache_id
* Cache id of the parent.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* The parent page to go back to.
*/
private function returnWithMessage($msg, $status, $cache_id) {
$this->messenger
->addStatus($msg, $status);
return $this
->redirect('entityconnect.return', [
'cache_id' => $cache_id,
'cancel' => TRUE,
]);
}
/**
* Makes sure our target id's are correct.
*
* @param array|int $target_id
* The target entity id.
*
* @return array|int
* The fixed target_id.
*/
private function fixTargetId($target_id) {
$array_target_id = is_array($target_id) ? $target_id : [
$target_id,
];
foreach ($array_target_id as $key => $value) {
if (!is_numeric($value) && is_string($value)) {
if ($value = EntityAutocomplete::extractEntityIdFromAutocompleteInput($value)) {
$array_target_id[$key] = $value;
}
}
}
return count($array_target_id) == 1 ? $array_target_id[0] : $array_target_id;
}
/**
* Returns the Symfony routes of the given entity's add form.
*
* @param string $entity_type
* The target entity type.
*
* @return array
* An array of add page routes for the given entity type.
*/
public static function getAddRoute($entity_type) {
/** @var \Drupal\Core\Routing\RouteProvider $route_provider */
$route_provider = \Drupal::getContainer()
->get('router.route_provider');
$route_name = [];
switch ($entity_type) {
case 'node':
$route_name[] = 'node.add';
break;
case 'user':
$route_name[] = 'user.admin_create';
break;
case 'shortcut':
$route_name[] = 'shortcut.link_add';
break;
default:
// Some default add form route names.
$route_name = [
$entity_type . '.add_form',
'entity.' . $entity_type . '.add_form',
];
}
return $route_provider
->getRoutesByNames($route_name);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
EntityconnectController:: |
protected | property | Temporary session storage for entityconnect. | |
EntityconnectController:: |
protected | property |
Drupal\Core\Messenger\MessengerInterface definition. Overrides MessengerTrait:: |
|
EntityconnectController:: |
protected | property | Drupal renderer. | |
EntityconnectController:: |
public | function | Add a new connecting entity. | |
EntityconnectController:: |
public static | function | Callback for creating the build array of entity types to add. | |
EntityconnectController:: |
public static | function |
Uses Symfony's ContainerInterface to declare dependency for constructor. Overrides ControllerBase:: |
|
EntityconnectController:: |
public | function | Page callback: Redirect to edit form. | |
EntityconnectController:: |
public static | function | Callback for creating the build array of entities to edit. | |
EntityconnectController:: |
private | function | Makes sure our target id's are correct. | |
EntityconnectController:: |
public static | function | Returns the Symfony routes of the given entity's add form. | |
EntityconnectController:: |
public | function | We redirect to the form page with the build_cache_id as a get param. | |
EntityconnectController:: |
private | function | Sets a message upon return to help with errors. | |
EntityconnectController:: |
public | function | Constructs a new EntityconnectController. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |