Skip to content
Snippets Groups Projects
Select Git revision
  • d2edf3eb65732defc05e95feca95efda0b40ebc7
  • 11.x default protected
  • 11.2.x protected
  • 10.6.x protected
  • 10.5.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

MapItem.php

Blame
  • catch's avatar
    Issue #3053363 by alexpott, Wim Leers, Berdir, mondrake, xjm: Remove support...
    catch authored
    Issue #3053363 by alexpott, Wim Leers, Berdir, mondrake, xjm: Remove support for PHP 5 in Drupal 8.8
    43cb1b5c
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    MapItem.php 2.34 KiB
    <?php
    
    namespace Drupal\Core\Field\Plugin\Field\FieldType;
    
    use Drupal\Core\Field\FieldStorageDefinitionInterface;
    use Drupal\Core\Field\FieldItemBase;
    
    /**
     * Defines the 'map' entity field type.
     *
     * @FieldType(
     *   id = "map",
     *   label = @Translation("Map"),
     *   description = @Translation("An entity field for storing a serialized array of values."),
     *   no_ui = TRUE,
     *   list_class = "\Drupal\Core\Field\MapFieldItemList",
     * )
     */
    class MapItem extends FieldItemBase {
    
      /**
       * {@inheritdoc}
       */
      public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
        // The properties are dynamic and can not be defined statically.
        return [];
      }
    
      /**
       * {@inheritdoc}
       */
      public static function schema(FieldStorageDefinitionInterface $field_definition) {
        return [
          'columns' => [
            'value' => [
              'type' => 'blob',
              'size' => 'big',
              'serialize' => TRUE,
            ],
          ],
        ];
      }
    
      /**
       * {@inheritdoc}
       */
      public function toArray() {
        // The default implementation of toArray() only returns known properties.
        // For a map, return everything as the properties are not pre-defined.
        return $this->getValue();
      }
    
      /**
       * {@inheritdoc}
       */
      public function setValue($values, $notify = TRUE) {
        $this->values = [];
        if (!isset($values)) {
          return;
        }
    
        if (!is_array($values)) {
          if ($values instanceof MapItem) {
            $values = $values->getValue();
          }
          else {
            $values = unserialize($values, ['allowed_classes' => FALSE]);
          }
        }
    
        $this->values = $values;
    
        // Notify the parent of any changes.
        if ($notify && isset($this->parent)) {
          $this->parent->onChange($this->name);
        }
      }
    
      /**
       * {@inheritdoc}
       */
      public function __get($name) {
        if (!isset($this->values[$name])) {
          $this->values[$name] = [];
        }
    
        return $this->values[$name];
      }
    
      /**
       * {@inheritdoc}
       */
      public function __set($name, $value) {
        if (isset($value)) {
          $this->values[$name] = $value;
        }
        else {
          unset($this->values[$name]);
        }
      }
    
      /**
       * {@inheritdoc}
       */
      public static function mainPropertyName() {
        // A map item has no main property.
        return NULL;
      }
    
      /**
       * {@inheritdoc}
       */
      public function isEmpty() {
        return empty($this->values);
      }
    
    }