/**
  * {@inheritdoc}
  */
 public function getValueFromProperty(FieldItemListInterface $property, $delta = 0)
 {
     if ($property->getValue() && $property->getFieldDefinition()->getCardinality() == 1) {
         return $property->referencedEntities()[0];
     }
     return $property->referencedEntities();
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $settings = $this->getSettings();
     $multiple = $this->fieldDefinition->getFieldStorageDefinition()->isMultiple();
     $values = $items->getValue();
     if ($multiple) {
         $default_value = array_column($values, 'target_id');
     } else {
         $default_value = !empty($values) ? $values[0]['target_id'] : NULL;
     }
     $element += ['#type' => 'entity_select', '#target_type' => $this->getFieldSetting('target_type'), '#multiple' => $multiple, '#default_value' => $default_value, '#autocomplete_threshold' => $settings['autocomplete_threshold'], '#autocomplete_size' => $settings['autocomplete_size'], '#autocomplete_placeholder' => $settings['autocomplete_placeholder'], '#required' => $this->fieldDefinition->isRequired()];
     return ['target_id' => $element];
 }
 /**
  * {@inheritdoc}
  */
 public function getValueFromProperty(FieldItemListInterface $property)
 {
     return $property->getValue();
 }
 /**
  * Process a field.
  *
  * @param \Drupal\Core\Field\FieldItemListInterface $field
  * @param string $op
  * @param boolean $force if set, we don't check if encryption is enabled, we process the field anyway. This is used during batch processes.
  */
 protected function process_field(\Drupal\Core\Field\FieldItemListInterface $field, $op = 'encrypt', $force = FALSE)
 {
     if (!is_callable([$field, 'getFieldDefinition'])) {
         return;
     }
     /**
      * @var $definition \Drupal\Core\Field\BaseFieldDefinition
      */
     $definition = $field->getFieldDefinition();
     if (!is_callable([$definition, 'get'])) {
         return;
     }
     $field_type = $definition->get('field_type');
     /**
      * Filter out fields that do not have a defined map.
      */
     if (!in_array($field_type, array_keys($this->getFieldEncryptMap()))) {
         return;
     }
     /**
      * @var $storage \Drupal\Core\Field\FieldConfigStorageBase
      */
     $storage = $definition->get('fieldStorage');
     if (is_null($storage)) {
         return;
     }
     /**
      * If we are using the force flag, we always proceed.
      * The force flag is used when we are updating stored fields.
      */
     if (!$force) {
         /**
          * Check if we are updating the field, in that case, skip it now (during
          * the initial entity load.
          */
         if ($this->updatingStoredField === $definition->get('field_name')) {
             return;
         }
         // Check if the field is encrypted.
         $encrypted = $storage->getThirdPartySetting('field_encrypt', 'encrypt', FALSE);
         if (!$encrypted) {
             return;
         }
     }
     /**
      * @var $field \Drupal\Core\Field\FieldItemList
      */
     $field_value = $field->getValue();
     foreach ($field_value as &$value) {
         // Process each of the sub fields that exits.
         $map = $this->fieldEncryptMap[$field_type];
         foreach ($map as $value_name => $service) {
             if (isset($value[$value_name])) {
                 $value[$value_name] = $this->process_value($value[$value_name], $service, $op);
             }
         }
     }
     // Set the new value.
     // We don't need to update the entity because the field setValue does that already.
     $field->setValue($field_value);
 }