3 namespace Drupal\metatag\Plugin\Field\FieldType;
5 use Drupal\Core\Field\FieldItemBase;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\Core\TypedData\DataDefinition;
10 * Plugin implementation of the 'metatag' field type.
14 * label = @Translation("Meta tags"),
15 * description = @Translation("This field stores code meta tags."),
16 * default_widget = "metatag_firehose",
17 * default_formatter = "metatag_empty_formatter"
20 class MetatagFieldItem extends FieldItemBase {
25 public static function schema(FieldStorageDefinitionInterface $field_definition) {
40 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
41 $properties['value'] = DataDefinition::create('metatag')
42 ->setLabel(t('Metatag'))
51 public function isEmpty() {
52 $value = $this->get('value')->getValue();
53 return $value === NULL || $value === '';
59 public function preSave() {
62 // Merge field defaults on top of global ones.
63 $default_tags = metatag_get_default_tags();
65 // Get the value about to be saved.
66 $current_value = $this->value;
67 $current_tags = unserialize($current_value);
69 // Only include values that differ from the default.
70 // @TODO: When site defaults are added, account for those.
72 foreach ($current_tags as $tag_id => $tag_value) {
73 if (!isset($default_tags[$tag_id]) || ($tag_value != $default_tags[$tag_id])) {
74 $tags_to_save[$tag_id] = $tag_value;
78 // Update the value to only save overridden tags.
79 $this->value = serialize($tags_to_save);