/**
  * {@inheritdoc}
  */
 public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition)
 {
     $default_value = parent::processDefaultValue($default_value, $entity, $definition);
     if ($default_value) {
         // Convert UUIDs to numeric IDs.
         $uuids = array();
         foreach ($default_value as $delta => $properties) {
             if (isset($properties['target_uuid'])) {
                 $uuids[$delta] = $properties['target_uuid'];
             }
         }
         if ($uuids) {
             $target_type = $definition->getSetting('target_type');
             $entity_ids = \Drupal::entityQuery($target_type)->condition('uuid', $uuids, 'IN')->execute();
             $entities = \Drupal::entityManager()->getStorage($target_type)->loadMultiple($entity_ids);
             $entity_uuids = array();
             foreach ($entities as $id => $entity) {
                 $entity_uuids[$entity->uuid()] = $id;
             }
             foreach ($uuids as $delta => $uuid) {
                 if (isset($entity_uuids[$uuid])) {
                     $default_value[$delta]['target_id'] = $entity_uuids[$uuid];
                     unset($default_value[$delta]['target_uuid']);
                 } else {
                     unset($default_value[$delta]);
                 }
             }
         }
         // Ensure we return consecutive deltas, in case we removed unknown UUIDs.
         $default_value = array_values($default_value);
     }
     return $default_value;
 }
Ejemplo n.º 2
-1
 /**
  * {@inheritdoc}
  */
 public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition)
 {
     // Explicitly call the base class so that we can get the default value
     // types.
     $default_value = FieldItemList::processDefaultValue($default_value, $entity, $definition);
     // Allow either the start or end date to have a default, but not require
     // defaults for both.
     if (!empty($default_value[0]['default_date_type']) || !empty($default_value[0]['default_end_date_type'])) {
         // A default value should be in the format and timezone used for date
         // storage. All-day ranges are stored the same as date+time ranges.  We
         // only provide a default value for the first item, as do all fields.
         // Otherwise, there is no way to clear out unwanted values on multiple
         // value fields.
         $storage_format = $definition->getSetting('datetime_type') == DateRangeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
         $default_values = [[]];
         if (!empty($default_value[0]['default_date_type'])) {
             $start_date = new DrupalDateTime($default_value[0]['default_date'], DATETIME_STORAGE_TIMEZONE);
             $start_value = $start_date->format($storage_format);
             $default_values[0]['value'] = $start_value;
             $default_values[0]['start_date'] = $start_date;
         }
         if (!empty($default_value[0]['default_end_date_type'])) {
             $end_date = new DrupalDateTime($default_value[0]['default_end_date'], DATETIME_STORAGE_TIMEZONE);
             $end_value = $end_date->format($storage_format);
             $default_values[0]['end_value'] = $end_value;
             $default_values[0]['end_date'] = $end_date;
         }
         $default_value = $default_values;
     }
     return $default_value;
 }
Ejemplo n.º 3
-1
 /**
  * {@inheritdoc}
  */
 public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition)
 {
     $default_value = parent::processDefaultValue($default_value, $entity, $definition);
     if (isset($default_value[0]['default_date_type'])) {
         // A default value should be in the format and timezone used for date
         // storage.
         $date = new DrupalDateTime($default_value[0]['default_date'], DATETIME_STORAGE_TIMEZONE);
         $storage_format = $definition->getSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
         $value = $date->format($storage_format);
         // We only provide a default value for the first item, as do all fields.
         // Otherwise, there is no way to clear out unwanted values on multiple value
         // fields.
         $default_value = array(array('value' => $value, 'date' => $date));
     }
     return $default_value;
 }