View source
<?php
namespace Drupal\schema_metatag\Plugin\metatag\Tag;
use Drupal\metatag\Plugin\metatag\Tag\MetaNameBase;
use Drupal\schema_metatag\SchemaMetatagManager;
use Drupal\schema_metatag\SchemaMetatagTestTagInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SchemaNameBase extends MetaNameBase implements SchemaMetatagTestTagInterface, ContainerFactoryPluginInterface {
protected $schemaMetatagManager;
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'));
return $instance;
}
public function setSchemaMetatagManager(SchemaMetatagManager $schemaMetatagManager) {
$this->schemaMetatagManager = $schemaMetatagManager;
}
protected function schemaMetatagManager() {
return $this->schemaMetatagManager;
}
protected function visibilitySelector() {
return $this
->getPluginId();
}
public function output() {
$value = $this
->schemaMetatagManager()
->unserialize($this
->value());
if (is_array($value)) {
$value = $this
->schemaMetatagManager()
->arrayTrim($value);
}
if (empty($value)) {
return '';
}
elseif (is_array($value)) {
array_walk_recursive($value, 'static::processItem');
$value = static::pivotItem($value);
}
else {
$this
->processItem($value);
}
$output = [
'#tag' => 'meta',
'#attributes' => [
'name' => $this->name,
'content' => static::outputValue($value),
'group' => $this->group,
'schema_metatag' => TRUE,
],
];
return $output;
}
public function value() {
return $this->value;
}
public function setValue($value) {
$this->value = $this
->schemaMetatagManager()
->serialize($value);
}
public static function pivotItem($array) {
if (array_key_exists('pivot', $array)) {
unset($array['pivot']);
$schemaMetatagManager = \Drupal::service('schema_metatag.schema_metatag_manager');
$array = $schemaMetatagManager
->pivot($array);
}
foreach ($array as &$value) {
if (is_array($value)) {
$value = static::pivotItem($value);
}
}
return $array;
}
protected function neverExplode() {
return [
'streetAddress',
'reviewBody',
'recipeInstructions',
];
}
protected function processItem(&$value, $key = 0) {
$explode = $key === 0 ? $this
->multiple() : !in_array($key, $this
->neverExplode());
$value = $this
->parseImageUrlValue($value, $explode);
$value = trim($value);
if ($this
->secure() && strpos($value, 'http://') !== FALSE) {
$value = str_replace('http://', 'https://', $value);
}
if ($explode) {
$value = $this
->schemaMetatagManager()
->explode($value);
if (is_array($value)) {
$value = array_filter($value);
}
}
}
protected function parseImageUrlValue($value, $explode) {
if ($this
->type() === 'image') {
global $base_root;
if (strpos($value, '<img src="/') !== FALSE) {
$value = str_replace('<img src="/', '<img src="' . $base_root . '/', $value);
}
if (strip_tags($value) != $value) {
if ($explode) {
$values = explode(',', $value);
}
else {
$values = [
$value,
];
}
foreach ($values as $key => $val) {
$matches = [];
preg_match('/src="([^"]*)"/', $val, $matches);
if (!empty($matches[1])) {
$values[$key] = $matches[1];
}
}
$value = implode(',', $values);
$value = strip_tags($value);
}
}
return $value;
}
public static function outputValue($input_value) {
return $input_value;
}
public static function testValue() {
return static::testDefaultValue(2, ' ');
}
public static function processedTestValue($items) {
return $items;
}
public static function processTestExplodeValue($items) {
if (!is_array($items)) {
$items = SchemaMetatagManager::explode($items);
if (is_array($items)) {
array_filter($items);
}
}
return $items;
}
public static 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[] = SchemaMetatagManager::randomMachineName();
}
return implode($delimiter, $items);
}
}