Ejemplo n.º 1
0
 /**
  * Maps from an entity object to the storage record.
  *
  * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  *   The entity object.
  * @param string $table_name
  *   (optional) The table name to map records to. Defaults to the base table.
  *
  * @return \stdClass
  *   The record to store.
  */
 protected function mapToStorageRecord(ContentEntityInterface $entity, $table_name = NULL)
 {
     if (!isset($table_name)) {
         $table_name = $this->baseTable;
     }
     $record = new \stdClass();
     $table_mapping = $this->getTableMapping();
     foreach ($table_mapping->getFieldNames($table_name) as $field_name) {
         if (empty($this->getFieldStorageDefinitions()[$field_name])) {
             throw new EntityStorageException("Table mapping contains invalid field {$field_name}.");
         }
         $definition = $this->getFieldStorageDefinitions()[$field_name];
         $columns = $table_mapping->getColumnNames($field_name);
         foreach ($columns as $column_name => $schema_name) {
             // If there is no main property and only a single column, get all
             // properties from the first field item and assume that they will be
             // stored serialized.
             // @todo Give field types more control over this behavior in
             //   https://www.drupal.org/node/2232427.
             if (!$definition->getMainPropertyName() && count($columns) == 1) {
                 $value = ($item = $entity->{$field_name}->first()) ? $item->getValue() : array();
             } else {
                 $value = isset($entity->{$field_name}->{$column_name}) ? $entity->{$field_name}->{$column_name} : NULL;
             }
             if (!empty($definition->getSchema()['columns'][$column_name]['serialize'])) {
                 $value = serialize($value);
             }
             // Do not set serial fields if we do not have a value. This supports all
             // SQL database drivers.
             // @see https://www.drupal.org/node/2279395
             $value = drupal_schema_get_field_value($definition->getSchema()['columns'][$column_name], $value);
             if (!(empty($value) && $this->isColumnSerial($table_name, $schema_name))) {
                 $record->{$schema_name} = $value;
             }
         }
     }
     return $record;
 }
 /**
  * Maps from an entity object to the storage record.
  *
  * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  *   The entity object.
  * @param string $table_name
  *   (optional) The table name to map records to. Defaults to the base table.
  *
  * @return \stdClass
  *   The record to store.
  */
 protected function mapToStorageRecord(ContentEntityInterface $entity, $table_name = NULL)
 {
     if (!isset($table_name)) {
         $table_name = $this->baseTable;
     }
     $record = new \stdClass();
     $table_mapping = $this->getTableMapping();
     foreach ($table_mapping->getFieldNames($table_name) as $field_name) {
         if (empty($this->getFieldStorageDefinitions()[$field_name])) {
             throw new EntityStorageException(String::format('Table mapping contains invalid field %field.', array('%field' => $field_name)));
         }
         $definition = $this->getFieldStorageDefinitions()[$field_name];
         $columns = $table_mapping->getColumnNames($field_name);
         foreach ($columns as $column_name => $schema_name) {
             // If there is no main property and only a single column, get all
             // properties from the first field item and assume that they will be
             // stored serialized.
             // @todo Give field types more control over this behavior in
             //   https://drupal.org/node/2232427.
             if (!$definition->getMainPropertyName() && count($columns) == 1) {
                 $value = $entity->{$field_name}->first()->getValue();
             } else {
                 $value = isset($entity->{$field_name}->{$column_name}) ? $entity->{$field_name}->{$column_name} : NULL;
             }
             if (!empty($definition->getSchema()['columns'][$column_name]['serialize'])) {
                 $value = serialize($value);
             }
             $record->{$schema_name} = drupal_schema_get_field_value($definition->getSchema()['columns'][$column_name], $value);
         }
     }
     return $record;
 }