class ItemListElement in Schema.org Metatag 8.2
Provides a plugin for the 'ItemListElement' Schema.org property type.
Plugin annotation
@SchemaPropertyType(
id = "item_list_element",
label = @Translation("ItemListElement"),
tree_parent = {
"ItemListElement",
},
tree_depth = -1,
property_type = "ItemListElement",
sub_properties = {},
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeBase implements ContainerFactoryPluginInterface, PropertyTypeInterface, SchemaMetatagTestTagInterface uses StringTranslationTrait
- class \Drupal\schema_metatag\Plugin\schema_metatag\PropertyType\ItemListElement
- class \Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeBase implements ContainerFactoryPluginInterface, PropertyTypeInterface, SchemaMetatagTestTagInterface uses StringTranslationTrait
Expanded class hierarchy of ItemListElement
File
- src/
Plugin/ schema_metatag/ PropertyType/ ItemListElement.php, line 24
Namespace
Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeView source
class ItemListElement extends PropertyTypeBase {
/**
* {@inheritdoc}
*/
public function form($input_values) {
$value = $input_values['value'];
$form = [
'#type' => 'textfield',
'#title' => $input_values['title'],
'#description' => $input_values['description'],
'#default_value' => !empty($value) ? $value : '',
'#maxlength' => 255,
];
$form['#description'] = $this
->t('To create a list, provide a token for a multiple value field, or a comma-separated list of values.');
$form['#description'] .= $this
->t("OR Provide the machine name of the view, and the machine name of the display, separated by a colon, i.e. 'view_name:display_id'. This will create a <a href=':url'>Summary View</a> list, which assumes each list item contains the url to a view page for the entity. The view rows should contain content (like teaser views) rather than fields for this to work correctly.", [
':url' => 'https://developers.google.com/search/docs/guides/mark-up-listings',
]);
return $form;
}
/**
* {@inheritdoc}
*/
public function outputValue($input_value) {
$items = [];
$values = $this
->getItems($input_value);
if (!empty($values) && is_array($values)) {
foreach ($values as $key => $value) {
if (is_array($value)) {
// Maps to Google all-in-one page view.
if (array_key_exists('@type', $value)) {
$items[] = [
'@type' => 'ListItem',
'position' => $key,
'item' => $value,
];
}
elseif (array_key_exists('url', $value)) {
$items[] = [
'@type' => 'ListItem',
'position' => $key,
'url' => $value['url'],
];
}
elseif (array_key_exists('name', $value) && array_key_exists('item', $value)) {
$items[] = [
'@type' => 'ListItem',
'position' => $key,
'name' => $value['name'],
'item' => $value['item'],
];
}
}
else {
$items[] = $value;
}
}
}
return $items;
}
/**
* {@inheritdoc}
*/
public function getItems($input_value) {
// A simple array of values.
$list = $this
->schemaMetatagManager()
->explode($input_value);
if (is_array($list)) {
return $list;
}
elseif (strpos(':', $input_value) !== FALSE) {
return $input_value;
}
else {
$values = [];
$args = explode(':', $input_value);
if (empty($args)) {
return $values;
}
// Load the requested view.
$view_id = array_shift($args);
$view = Views::getView($view_id);
// Set the display.
if (count($args) > 0) {
$display_id = array_shift($args);
$view
->setDisplay($display_id);
}
else {
$view
->initDisplay();
}
// See if the page's arguments should be passed to the view.
if (count($args) == 1 && $args[0] == '{{args}}') {
$view_path = explode("/", $view
->getPath());
$current_url = Url::fromRoute('<current>');
$query_args = explode("/", substr($current_url
->toString(), 1));
$args = [];
foreach ($query_args as $index => $arg) {
if (in_array($arg, $view_path)) {
unset($query_args[$index]);
}
}
if (!empty($query_args)) {
$args = array_values($query_args);
}
}
// Allow modules to alter the arguments passed to the view.
\Drupal::moduleHandler()
->alter('schema_item_list_views_args', $args);
if (!empty($args)) {
$view
->setArguments($args);
}
$view
->preExecute();
$view
->execute();
// Get the view results.
$key = 1;
foreach ($view->result as $item) {
// If this is a display that does not provide an entity in the result,
// there is really nothing more to do.
$entity = static::getEntityFromRow($item);
if (!$entity) {
return '';
}
// Get the absolute path to this entity.
$url = $entity
->toUrl()
->setAbsolute()
->toString();
$values[$key] = [
'@id' => $url,
'name' => $entity
->label(),
'url' => $url,
];
$key++;
}
}
return $values;
}
/**
* Tries to retrieve an entity from a Views row.
*
* @param object $row
* The Views row.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity or NULL.
*/
protected static function getEntityFromRow($row) {
if (!empty($row->_entity)) {
return $row->_entity;
}
if (isset($row->_object) && $row->_object instanceof EntityAdapter) {
return $row->_object
->getValue();
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function testValue($type = '') {
return 'first,second';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ItemListElement:: |
public | function |
Create a complete form element for this property type. Overrides PropertyTypeBase:: |
1 |
ItemListElement:: |
protected static | function | Tries to retrieve an entity from a Views row. | |
ItemListElement:: |
public | function | 1 | |
ItemListElement:: |
public | function |
Transform input value to its display output. Overrides PropertyTypeBase:: |
1 |
ItemListElement:: |
public | function |
Provide a test input value for the property that will validate. Overrides PropertyTypeBase:: |
1 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
PropertyTypeBase:: |
protected | property | The propertyTypeManager service. | |
PropertyTypeBase:: |
protected | property | The SchemaMetatagClient service. | |
PropertyTypeBase:: |
protected | property | The schemaMetatagManager service. | |
PropertyTypeBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
PropertyTypeBase:: |
public | function |
A property form element. Overrides PropertyTypeInterface:: |
5 |
PropertyTypeBase:: |
public | function |
Get an instance of a child property type. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
Create an option list for a given tree section. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
The property type. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
The sub-properties. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
Get some or all of the object tree as options for @type. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
The depth of the class tree to use for @type options. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
The classes to use for the @type options of this property. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
Construct the visibility selector for a set of values. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
Pivot form element. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
Provide a test output value for the input value. Overrides SchemaMetatagTestTagInterface:: |
|
PropertyTypeBase:: |
public | function |
Explode a test value. Overrides SchemaMetatagTestTagInterface:: |
|
PropertyTypeBase:: |
public | function |
Get all the properties of a property type. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
The Schema Metatag Client service. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function |
The Schema Metatag Manager service. Overrides PropertyTypeInterface:: |
|
PropertyTypeBase:: |
public | function | Sets PropertyTypeManager service. | |
PropertyTypeBase:: |
public | function | Sets SchemaMetatagClient service. | |
PropertyTypeBase:: |
public | function | Sets schemaMetatagManager service. | |
PropertyTypeBase:: |
public | function |
Provide a random test value. Overrides SchemaMetatagTestTagInterface:: |
|
PropertyTypeBase:: |
public static | function |
Validates the property form when submitted. Overrides PropertyTypeInterface:: |
|
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. |