class PropertyTypeBase in Schema.org Metatag 8.2
Base class for Property type plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeBase implements ContainerFactoryPluginInterface, PropertyTypeInterface, SchemaMetatagTestTagInterface uses StringTranslationTrait
Expanded class hierarchy of PropertyTypeBase
42 files declare their use of PropertyTypeBase
- Action.php in src/
Plugin/ schema_metatag/ PropertyType/ Action.php - AggregateRating.php in src/
Plugin/ schema_metatag/ PropertyType/ AggregateRating.php - Answer.php in src/
Plugin/ schema_metatag/ PropertyType/ Answer.php - Book.php in src/
Plugin/ schema_metatag/ PropertyType/ Book.php - Boolean.php in src/
Plugin/ schema_metatag/ PropertyType/ Boolean.php
File
- src/
Plugin/ schema_metatag/ PropertyTypeBase.php, line 17
Namespace
Drupal\schema_metatag\Plugin\schema_metatagView source
class PropertyTypeBase extends PluginBase implements PropertyTypeInterface, SchemaMetatagTestTagInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* The schemaMetatagManager service.
*
* @var \Drupal\schema_metatag\SchemaMetatagManagerInterface
*/
protected $schemaMetatagManager;
/**
* The SchemaMetatagClient service.
*
* @var \Drupal\schema_metatag\SchemaMetatagClientInterface
*/
protected $schemaMetatagClient;
/**
* The propertyTypeManager service.
*
* @var \Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeManager
*/
protected $propertyTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance
->setSchemaMetatagManager($container
->get('schema_metatag.schema_metatag_manager'));
$instance
->setSchemaMetatagClient($container
->get('schema_metatag.schema_metatag_client'));
$instance
->setPropertyTypeManager($container
->get('plugin.manager.schema_property_type'));
return $instance;
}
/**
* Sets schemaMetatagManager service.
*
* @param \Drupal\schema_metatag\SchemaMetatagManagerInterface $schemaMetatagManager
* The Schema Metatag Manager service.
*/
public function setSchemaMetatagManager(SchemaMetatagManagerInterface $schemaMetatagManager) {
$this->schemaMetatagManager = $schemaMetatagManager;
}
/**
* Sets SchemaMetatagClient service.
*
* @param \Drupal\schema_metatag\SchemaMetatagClientInterface $schemaMetatagClient
* The Schema.org client.
*/
public function setSchemaMetatagClient(SchemaMetatagClientInterface $schemaMetatagClient) {
$this->schemaMetatagClient = $schemaMetatagClient;
}
/**
* Sets PropertyTypeManager service.
*
* @param \Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeManager $propertyTypeManager
* The property type manager.
*/
public function setPropertyTypeManager(PropertyTypeManager $propertyTypeManager) {
$this->propertyTypeManager = $propertyTypeManager;
}
/**
* {@inheritdoc}
*/
public function schemaMetatagManager() {
return $this->schemaMetatagManager;
}
/**
* {@inheritdoc}
*/
public function schemaMetatagClient() {
return $this->schemaMetatagClient;
}
/**
* {@inheritdoc}
*/
public function getTreeParent() {
return !empty($this->pluginDefinition['tree_parent']) ? $this->pluginDefinition['tree_parent'] : [];
}
/**
* {@inheritdoc}
*/
public function getTreeDepth() {
return !empty($this->pluginDefinition['tree_depth']) ? $this->pluginDefinition['tree_depth'] : -1;
}
/**
* {@inheritdoc}
*/
public function getPropertyType() {
return $this->pluginDefinition['property_type'];
}
/**
* {@inheritdoc}
*/
public function getSubProperties() {
return !empty($this->pluginDefinition['sub_properties']) ? $this->pluginDefinition['sub_properties'] : [];
}
/**
* {@inheritdoc}
*/
public function propertyInfo($property_type, $with_parents = TRUE) {
$all_properties = $this
->schemaMetatagClient()
->getProperties();
$properties = [];
$parents = [
$property_type,
];
if ($with_parents) {
$parents += $this
->schemaMetatagClient()
->getParents($property_type);
}
foreach ($parents as $type) {
$properties = array_merge($properties, $all_properties[$type]);
}
return $properties;
}
/**
* {@inheritdoc}
*/
public function getTree($parent, $depth) {
$tree = [];
foreach ((array) $parent as $item) {
$tree = array_merge($this->schemaMetatagClient
->getTree($item, $depth), $tree);
}
return $tree;
}
/**
* {@inheritdoc}
*/
public function getOptionList($parent, $depth) {
$list = [];
foreach ((array) $parent as $item) {
$list = array_merge($this->schemaMetatagClient
->getOptionList($item, $depth), $list);
}
return $list;
}
/**
* {@inheritdoc}
*/
public function form(array $input_values) {
$default_values = $this
->schemaMetatagManager()
->defaultInputValues();
$input_values = array_merge($default_values, $input_values);
// If no tree values were supplied, default to the values of the property
// type plugin.
if (empty($input_values['tree_parent'])) {
$input_values['tree_parent'] = $this
->getTreeParent();
$input_values['tree_depth'] = $this
->getTreeDepth();
}
// The properties and property types to generate for this form.
$sub_properties = $this
->getSubProperties();
if (!empty($sub_properties)) {
$form['#type'] = 'fieldset';
$form['#title'] = $input_values['title'];
$form['#description'] = $input_values['description'];
$form['#tree'] = TRUE;
// For each sub property, generate a form element for the sub property
// by invoking an instance of that child property type.
foreach ($sub_properties as $sub_property_name => $values) {
$sub_property_type = $values['id'];
$child_property = $this
->getChildPropertyType($sub_property_type);
$sub_property_value = is_array($input_values['value']) && array_key_exists($sub_property_name, $input_values['value']) ? $input_values['value'][$sub_property_name] : NULL;
$sub_input_values['title'] = $values['label'];
$sub_input_values['description'] = $values['description'];
$sub_input_values['value'] = $sub_property_value;
$sub_input_values['visibility_selector'] = $input_values['visibility_selector'];
if (!empty($values['tree_parent'])) {
$sub_input_values['visibility_selector'] .= "[{$sub_property_name}]";
}
// Pass parent tree values when empty, otherwise give each sub property
// its own tree values.
$sub_input_values['tree_parent'] = empty($values['tree_parent']) ? $input_values['tree_parent'] : $values['tree_parent'];
$sub_input_values['tree_depth'] = empty($values['tree_depth']) ? $input_values['tree_depth'] : $values['tree_depth'];
// Generate the sub property form element.
$form[$sub_property_name] = $child_property
->form($sub_input_values);
if ($sub_property_name != '@type') {
// Add #states to hide this whole section if @type is empty.
$form[$sub_property_name]['#states'] = $this
->getVisibility($input_values);
}
else {
// Add a pivot element to the top of multiple value forms.
if (!empty($input_values['multiple'])) {
$value = is_array($input_values['value']) && array_key_exists('pivot', $input_values['value']) ? $input_values['value']['pivot'] : 0;
$form['pivot'] = $this
->pivotForm($value);
$form['pivot']['#states'] = $this
->getVisibility($input_values);
}
}
}
}
else {
$form = $this
->formElement($input_values);
}
$form['#element_validate'] = [
[
get_class($this),
'validateProperty',
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function formElement(array $input_values) {
$value = $input_values['value'];
$form['#type'] = 'textfield';
$form['#title'] = $input_values['title'];
$form['#description'] = $input_values['description'];
$form['#default_value'] = !empty($value) ? $value : '';
$form['#maxlength'] = 255;
return $form;
}
/**
* {@inheritdoc}
*/
public function pivotForm($value) {
$form = [
'#type' => 'select',
'#title' => 'Pivot',
'#default_value' => $value,
'#empty_option' => ' - ' . $this
->t('None') . ' - ',
'#empty_value' => '',
'#options' => [
1 => $this
->t('Pivot'),
],
'#description' => $this
->t('Combine and pivot multiple values to display them as multiple objects.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function getVisibility(array $input_values) {
$selector = ':input[name="' . $input_values['visibility_selector'] . '[@type]"]';
$visibility = [
'invisible' => [
$selector => [
'value' => '',
],
],
];
$selector2 = $this
->schemaMetatagManager()
->altSelector($selector);
$visibility2 = [
'invisible' => [
$selector2 => [
'value' => '',
],
],
];
$visibility['invisible'] = [
$visibility['invisible'],
$visibility2['invisible'],
];
return $visibility;
}
/**
* {@inheritdoc}
*/
public static function validateProperty(array &$element, FormStateInterface $form_state) {
// Extend as needed to validate property types.
}
/**
* {@inheritdoc}
*/
public function getChildPropertyType($plugin_id) {
return $this->propertyTypeManager
->createInstance($plugin_id);
}
/**
* {@inheritdoc}
*/
public function outputValue($input_value) {
return $input_value;
}
/**
* {@inheritdoc}
*/
public function testValue($original_type = '') {
if (empty($this
->getSubProperties())) {
return self::testDefaultValue(2, ' ');
}
else {
$items = [];
foreach ($this
->getSubProperties() as $property_name => $values) {
$plugin = $this
->getChildPropertyType($values['id']);
if ($property_name == '@type') {
$items[$property_name] = $plugin
->testValue($original_type);
}
else {
$type = !empty($values['tree_parent']) ? $values['tree_parent'] : $plugin
->getTreeParent();
$test_type = is_array($type) ? array_shift($type) : $type;
$items[$property_name] = $plugin
->testValue($test_type);
}
}
return $items;
}
}
/**
* {@inheritdoc}
*/
public function processedTestValue($items) {
if (empty($this
->getSubProperties())) {
return $this
->processTestExplodeValue($items);
}
else {
foreach ($this
->getSubProperties() as $property_name => $values) {
$items[$property_name] = $this
->getChildPropertyType($values['id'])
->processedTestValue($items[$property_name]);
}
}
return $items;
}
/**
* {@inheritdoc}
*/
public function processTestExplodeValue($items) {
if (!is_array($items)) {
$items = $this
->schemaMetatagManager()
->explode($items);
// Clean out any empty values that might have been added by explode().
if (is_array($items)) {
array_filter($items);
}
}
return $items;
}
/**
* {@inheritdoc}
*/
public function testDefaultValue($count = NULL, $delimiter = NULL) {
$items = [];
$min = 1;
$max = isset($count) ? $count : 2;
$delimiter = isset($delimiter) ? $delimiter : ' ';
for ($i = $min; $i <= $max; $i++) {
$items[] = $this
->schemaMetatagManager()
->randomMachineName();
}
return implode($delimiter, $items);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 |
Create a complete form element for this property type. Overrides PropertyTypeInterface:: |
1 |
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 |
Transform input value to its display output. Overrides PropertyTypeInterface:: |
2 |
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 | function |
Provide a test input value for the property that will validate. Overrides SchemaMetatagTestTagInterface:: |
3 |
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. |