/** * Save field configuration to database. * * @see \field_update_field(). * @see \field_create_field(). */ public function save() { if (isset($this->id)) { \field_update_field((array) $this); } else { foreach (\field_create_field((array) $this) as $k => $v) { $this->{$k} = $v; } } return $this; }
public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false) { /* @var $node FieldNode */ $object = $node->getValue(); if (!is_array($object)) { $object = array(); } if (!isset($object['type'])) { $context->logCritical(sprintf("%s: has no type", $node->getPath())); } $name = $node->getName(); $type = $object['type']; $typeInfo = field_info_field_types($type); if (empty($typeInfo)) { $context->logCritical(sprintf("%s: type %s does not exist", $node->getPath(), $type)); } if ($this->exists($node, $context)) { $existing = $this->getExistingObject($node, $context); } else { $existing = null; } if (array_key_exists('settings', $object) && !is_array($object['settings'])) { $context->log(sprintf("%s: no settings provided, defaulting with empty array", $node->getPath())); $object['settings'] = array(); } $object['field_name'] = $name; if (empty($object['cardinality'])) { $object['cardinality'] = 1; } // Consistency check, prior to do anything, ensure the database tables // are not already there, this happens, sometimes if (!$existing) { if (empty($object['storage']) || 'field_sql_storage' === $object['storage']['type']) { // Prevents warning on unspecified key if (!isset($object['deleted'])) { $object['deleted'] = false; } $tables = [_field_sql_storage_tablename($object), _field_sql_storage_revision_tablename($object)]; foreach ($tables as $table) { if (db_table_exists($table)) { $context->logDataloss(sprintf("%s: %s: table already exists prior to creating field", $node->getPath(), $table)); // If code has not broken here, then go for deletion db_drop_table($table); } } } } if ($existing) { $doDelete = false; $eType = $existing['type']; // Ensure the cardinality change if any is safe to proceed with $cardinality = $object['cardinality'] - $existing['cardinality']; if (0 !== $cardinality) { if (0 < $cardinality || -1 == $object['cardinality']) { $context->log(sprintf("%s: safe cardinality change", $node->getPath())); } else { // @todo Ensure there is data we can save in field if (false) { $context->log(sprintf("%s: safe cardinality change due to data shape", $node->getPath())); } else { $context->logDataloss(sprintf("%s: unsafe cardinality change", $node->getPath())); } } } if ($type !== $eType) { $doDelete = true; $instances = $this->getInstances($name); if (empty($instances)) { $context->logWarning(sprintf("%s: type change (%s -> %s): no instances", $node->getPath(), $type, $eType)); } else { // @todo Ensure there is data if there is instances if (false) { $context->logWarning(sprintf("%s: type change (%s -> %s): existing instances are empty", $node->getPath(), $type, $eType)); } else { // @todo Safe should ensure schema is the same if (false) { $context->logWarning(sprintf("%s: type change (%s -> %s): field schema is the same", $node->getPath(), $type, $eType)); } else { $context->logDataloss(sprintf("%s: type change (%s -> %s): data loss detected", $node->getPath(), $type, $eType)); } } } } if ($doDelete) { $this->deleteExistingObject($node, $context); field_create_field($object); // @todo Recreate instances } else { field_update_field($object); } } else { field_create_field($object); } }
/** * Enable field translation. * * @param string $field_name * Field machine name. */ public function enableFieldTranslation($field_name) { $info = field_info_field($field_name); $info['translatable'] = TRUE; field_update_field($info); }
/** * Implements Drupal\configuration\Config\Configuration::saveToActiveStore(). */ public function saveToActiveStore(ConfigIteratorSettings &$settings) { field_info_cache_clear(); // Load all the existing fields and instance up-front so that we don't // have to rebuild the cache all the time. $existing_fields = field_info_fields(); $existing_instances = field_info_instances(); $field = $this->getData(); // Create or update field. $field_config = $field['field_config']; if (isset($existing_fields[$field_config['field_name']])) { $existing_field = $existing_fields[$field_config['field_name']]; if ($field_config + $existing_field != $existing_field) { field_update_field($field_config); } } else { field_create_field($field_config); $existing_fields[$field_config['field_name']] = $field_config; } // Create or update field instance. $field_instance = $field['field_instance']; if (isset($existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']])) { $existing_instance = $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']]; if ($field_instance + $existing_instance != $existing_instance) { field_update_instance($field_instance); } } else { field_create_instance($field_instance); $existing_instances[$field_instance['entity_type']][$field_instance['bundle']][$field_instance['field_name']] = $field_instance; } variable_set('menu_rebuild_needed', TRUE); $settings->addInfo('imported', $this->getUniqueId()); }