View source
<?php
class SchemaNameBase extends DrupalTextMetaTag implements SchemaMetatagTestTagInterface {
protected $schemaMetatagManager;
function __construct(array $info, array $data = NULL) {
parent::__construct($info, $data);
$this->schemaMetatagManager = new SchemaMetatagManager();
}
protected function schemaMetatagManager() {
return $this->schemaMetatagManager;
}
public function t($str, $args = []) {
return t($str, $args);
}
public function getPluginId() {
return $this->info['name'];
}
public function label() {
return $this->info['label'];
}
public function description() {
return $this->info['description'];
}
public function multiple() {
return !empty($this->info['multiple']);
}
public function value() {
return !empty($this->data['value']) ? $this->data['value'] : '';
}
protected function visibilitySelector() {
return 'metatags[und][' . $this->info['name'] . '][value]';
}
public function getForm(array $options = array()) {
$form = parent::getForm($options);
$form['value']['#element_validate'][] = 'schema_metatag_element_validate';
return $form;
}
public function getElement(array $options = array()) {
$this->options = $options;
$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);
}
$parts = explode('.', $this->info['name']);
$id = 'schema_metatag_' . $this->info['name'];
$element = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'schema_metatag' => TRUE,
'group' => $parts[0],
'name' => $parts[1],
'content' => static::outputValue($value),
],
];
return array(
'#attached' => array(
'drupal_add_html_head' => array(
array(
$element,
$id,
),
),
),
);
}
public static function pivotItem($array) {
if (array_key_exists('pivot', $array)) {
unset($array['pivot']);
$array = $this
->schemaMetatagManager()
->pivot($array);
}
foreach ($array as $key => &$value) {
if (is_array($value)) {
$value = static::pivotItem($value);
}
}
return $array;
}
public static function neverExplode() {
return [
'streetAddress',
'reviewBody',
'recipeInstructions',
];
}
protected function processItem(&$value, $key = 0) {
$explode = $key === 0 ? $this
->multiple() : !in_array($key, static::neverExplode());
$backup_data = $this->data;
$backup_info = $this->info;
$this->data['value'] = $value;
if (!empty($this->info['url'])) {
$this->info['url'] = $this->info['url'] && in_array($key, [
'url',
'sameAs',
]);
}
if (!empty($this->info['image'])) {
$this->info['image'] = $this->info['image'] && in_array($key, [
'url',
]);
}
$value = $this
->getValue($this->options);
if ($explode) {
$value = $this
->schemaMetatagManager()
->explode($value);
if (is_array($value)) {
$value = array_filter($value);
}
}
$this->data = $backup_data;
$this->info = $backup_info;
}
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 randomUrl() {
return 'http://google.com/' . static::testDefaultValue(1, '');
}
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);
}
}