class MetatagViewsController in Metatag 8
Class MetatagViewsController.
@package Drupal\metatag_views\Controller
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\metatag_views\Controller\MetatagViewsController
Expanded class hierarchy of MetatagViewsController
1 file declares its use of MetatagViewsController
- MetatagViewsAddForm.php in metatag_views/
src/ Form/ MetatagViewsAddForm.php
File
- metatag_views/
src/ Controller/ MetatagViewsController.php, line 17
Namespace
Drupal\metatag_views\ControllerView source
class MetatagViewsController extends ControllerBase {
/**
* The Views storage interface.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $viewStorage;
/**
* The Metatag manager interface.
*
* @var \Drupal\metatag\MetatagManagerInterface
*/
protected $metatagManager;
/**
* Associative array of labels.
*
* @var array
*/
protected $viewLabels;
/**
* {@inheritdoc}
*/
public function __construct(EntityStorageInterface $viewStorage, MetatagManagerInterface $metatagManager) {
$this->viewStorage = $viewStorage;
$this->metatagManager = $metatagManager;
// Generate the labels for views and displays.
$this->labels = $this
->getViewsAndDisplaysLabels();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('view'), $container
->get('metatag.manager'));
}
/**
* Get meta tags for all of the views / displays that have them set.
*
* @return array
* List of tags grouped by view and display.
*/
public static function getTaggedViews() {
$tagged_views = [];
foreach (Views::getEnabledViews() as $view_id => $view) {
$displays = $view
->get('display');
foreach (array_keys($displays) as $display_id) {
if ($tags = metatag_get_view_tags($view_id, $display_id)) {
$tagged_views[$view_id][$display_id] = $tags;
}
}
}
return $tagged_views;
}
/**
* Generates the renderable array for views meta tags UI.
*
* @return array
* The list of details.
*/
public function listViews() {
$elements = [];
$elements['header'] = [
'#markup' => '<p>' . $this
->t("To view a list of displays with meta tags set up, click on a view name. To view a summary of meta tags configuration for a particular display, click on the display name. If you need to set meta tags for a specific view, choose Add views meta tags. Reverting the meta tags removes the specific configuration and falls back to defaults.") . '</p>',
];
// Iterate over the values and build the whole UI.
// 1. Top level is a collapsible fieldset with a view name (details)
// 2. Inside each fieldset we have 2 columns -> Display and Operations.
// Display contains point 3.
// Operations contain edit and revert.
// 3. In each display there is a table that has 2 columns: tag name and tag
// value.
$tagged_views = $this
->getTaggedViews();
foreach ($tagged_views as $view_id => $displays) {
$elements[$view_id] = [
'#type' => 'details',
'#title' => $this
->t($this->viewLabels[$view_id]['#label']),
'details' => $this
->buildViewDetails($view_id, $displays),
];
}
return $elements;
}
/**
* Builds the second "level" of the UI table with display fieldset and ops.
*
* @param string $view_id
* The view display to use.
* @param array $displays
* The displays to process.
*
* @return array
* Render array.
*/
protected function buildViewDetails($view_id, array $displays) {
$element = [
'#type' => 'table',
'#collapsible' => TRUE,
'#header' => [
$this
->t('Display'),
$this
->t('Operations'),
],
];
foreach ($displays as $display_id => $metatags) {
$metatags = array_filter($metatags);
$element[$display_id]['details'] = [
'#type' => 'details',
'#title' => $this->viewLabels[$view_id][$display_id],
];
$params = [
'view_id' => $view_id,
'display_id' => $display_id,
];
// Generate the operations.
$element[$display_id]['ops'] = [
'#type' => 'operations',
'#links' => [
'edit' => [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('metatag_views.metatags.edit', $params),
],
'translate' => [
'title' => $this
->t('Translate'),
'url' => Url::fromRoute('metatag_views.metatags.translate_overview', $params),
],
'revert' => [
'title' => $this
->t('Revert'),
'url' => Url::fromRoute('metatag_views.metatags.revert', $params),
],
],
];
// Build the rows for each of the metatag types.
$element[$display_id]['details']['table'] = $this
->buildDisplayDetailsTable($metatags);
}
return $element;
}
/**
* Build the table with metatag values summary.
*
* @param array $tags
* The tags to process.
*
* @return array
* The tag structure in a display element.
*/
protected function buildDisplayDetailsTable(array $tags) {
$element = [
'#type' => 'table',
];
$i = 0;
foreach ($tags as $tag_name => $tag_value) {
// This is for the case where we have a subarray.
$tag_value = $this
->prepareTagValue($tag_value);
if (!$tag_value) {
continue;
}
$element[$i]['tag_name'] = [
'#type' => 'markup',
'#markup' => $tag_name,
];
$element[$i]['tag_value'] = [
'#type' => 'markup',
'#markup' => $tag_value,
];
$i++;
}
return $element;
}
/**
* Massage the tag value.
*
* @param string $value
* The meta tag to output.
*
* @return string
* An imploded string for meta tags that are nested, ex. robots.
*/
protected function prepareTagValue($value) {
if (is_array($value)) {
$value = implode(', ', array_filter($value));
}
return $value;
}
/**
* Gets label values for the views and their displays.
*/
protected function getViewsAndDisplaysLabels() {
/** @var \Drupal\views\ViewEntityInterface[] $views */
$views = $this->viewStorage
->loadByProperties([
'status' => 1,
]);
$labels = [];
foreach ($views as $view_id => $view) {
$displays = $view
->get('display');
$labels[$view_id]['#label'] = $view
->label();
foreach (array_keys($displays) as $display_id) {
$labels[$view_id][$display_id] = $displays[$display_id]['display_title'];
}
}
$this->viewLabels = $labels;
}
}
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. | |
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:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
MetatagViewsController:: |
protected | property | The Metatag manager interface. | |
MetatagViewsController:: |
protected | property | Associative array of labels. | |
MetatagViewsController:: |
protected | property | The Views storage interface. | |
MetatagViewsController:: |
protected | function | Build the table with metatag values summary. | |
MetatagViewsController:: |
protected | function | Builds the second "level" of the UI table with display fieldset and ops. | |
MetatagViewsController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
MetatagViewsController:: |
public static | function | Get meta tags for all of the views / displays that have them set. | |
MetatagViewsController:: |
protected | function | Gets label values for the views and their displays. | |
MetatagViewsController:: |
public | function | Generates the renderable array for views meta tags UI. | |
MetatagViewsController:: |
protected | function | Massage the tag value. | |
MetatagViewsController:: |
public | function | ||
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. |