protected function getFieldDefault(NodeInterface $node, $entityType, $bundle, $fieldName, Context $context) { /* @var $node ViewNode */ $entityType = $node->getEntityType(); $bundle = $node->getBundle(); $default = array('type' => 'hidden', 'label' => 'hidden', 'settings' => array()); if (!($field = field_info_field($fieldName))) { // @todo Should we warn? This is surely an extra field. return $default; } if (!($field = field_info_field_types($field['type']))) { $context->logError("%s: %s field type does not exist", $node->getPath(), $field['types']); return $default; } $formatter = null; if (!empty($field['default_formatter'])) { $formatter = $field['default_formatter']; if (!field_info_formatter_types($formatter)) { $context->logWarning(sprintf("%s: field %s defines non existing default formatter: %s", $node->getPath(), $fieldName, $formatter)); $formatter = null; } } if ($formatter) { $default = array('type' => $formatter, 'label' => 'hidden', 'settings' => field_info_formatter_settings($formatter)); } return $default; }
/** * Load default data for this field-type. * * @see \field_create_field(). */ public function setType($type) { $field_type = \field_info_field_types($type); $this->settings += \field_info_field_settings($type); $this->module = $field_type['module']; $this->type = $type; }
/** * @Then /^I should have a "([^"]*)" field as a "([^"]*)" type, has a "([^"]*)" widget, (not required|required), and allows (\d+|(?i)unlimited) value[s]?[.]?$/ */ public function iShouldHaveAFieldAsTypeHasAWidgetRequiredAndAllowsValue($name, $type, $widget, $required, $cardinality) { if (strcasecmp($cardinality, 'unlimited') == 0) { $cardinality = '-1'; } $fields = field_info_instances("node", $this->contentType->type); $wantedField = NULL; foreach ($fields as $field) { if ($field['label'] == $name) { $wantedField = $field; } } assertNotEmpty($wantedField, "Field with the label {$name} doesn't exist"); $fieldInfo = field_info_field($wantedField['field_name']); $widgetInfo = field_info_widget_types(); $fieldTypeInfo = field_info_field_types(); $wantedField['widget']['info'] = $widgetInfo[$wantedField['widget']['type']]; $wantedField['type'] = $fieldTypeInfo[$fieldInfo['type']]; assertEquals($type, $wantedField['type']['label'], "{$name} doesn't have the type {$type}. Instead it has " . $wantedField['type']['label'] . '.'); assertEquals($widget, $wantedField['widget']['info']['label'], "{$name} doesn't have the widget type {$widget}. Instead it has " . $wantedField['widget']['info']['label'] . '.'); $fieldRequired = $wantedField['required'] ? 'required' : 'not required'; assertEquals($required, $fieldRequired, "{$name} is marked '{$fieldRequired}'. It should be '{$required}'."); assertEquals($cardinality, $fieldInfo['cardinality'], "{$name} allows " . $fieldInfo['cardinality'] . " values. It should only allow {$cardinality} values."); $this->fieldList[] = $wantedField['field_name']; }
/** * Set field and update default values accordingly. * * @see _field_write_instance(). */ public function setField(Field $field) { $this->field = $field; $this->settings += \field_info_instance_settings($field->type); $field_type = \field_info_field_types($field->type); if (!isset($this->widget['type'])) { $this->setWidget($field_type['default_widget']); } foreach ($this->display as $view_mode => &$settings) { if (!isset($settings['type'])) { $this->setFormatter($view_mode, isset($field_type['default_formatter']) ? $field_type['default_formatter'] : 'hidden'); } } }
public static function getFieldTypes($type = null) { return field_info_field_types($type); }
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); } }
function option_definition() { $options = parent::option_definition(); // option_definition runs before init/construct, so no $this->field_info $field = field_info_field($this->definition['subfield_name']); $field_type = field_info_field_types($field['type']); $column_names = array_keys($field['columns']); $default_column = ''; // Try to determine a sensible default. if (count($column_names) == 1) { $default_column = $column_names[0]; } elseif (in_array('value', $column_names)) { $default_column = 'value'; } // If the field has a "value" column, we probably need that one. $options['click_sort_column'] = array('default' => $default_column); $options['type'] = array('default' => $field_type['default_formatter']); $options['settings'] = array('default' => array()); $options['group_column'] = array('default' => $default_column); $options['group_columns'] = array('default' => array()); // Options used for multiple value fields. $options['group_rows'] = array('default' => TRUE, 'bool' => TRUE); // If we know the exact number of allowed values, then that can be // the default. Otherwise, default to 'all'. $options['delta_limit'] = array('default' => $field['cardinality'] > 1 ? $field['cardinality'] : 'all'); $options['delta_offset'] = array('default' => 0); $options['delta_reversed'] = array('default' => FALSE, 'bool' => TRUE); $options['delta_first_last'] = array('default' => FALSE, 'bool' => TRUE); $options['multi_type'] = array('default' => 'separator'); $options['separator'] = array('default' => ', '); $options['field_api_classes'] = array('default' => FALSE, 'bool' => TRUE); return $options; }
public static function changeInstances(array $field, array $field_instance_overrides = array()) { $type_info = field_info_field_types($field['type']); $instances = field_read_instances(array('field_name' => $field['field_name'])); foreach ($instances as $instance) { $prior_instance = $instance; // Serialize properties back into the data property so it can be saved // to the database. $instance['data'] = array(); foreach ($instance as $key => $value) { switch ($key) { case 'id': case 'field_id': case 'field_name': case 'entity_type': case 'bundle': case 'deleted': case 'data': break; default: $instance['data'][$key] =& $instance[$key]; } } $instance['settings'] = array_intersect_key($instance['settings'], $type_info['instance_settings']); $instance['settings'] += $type_info['instance_settings']; // Validate the existing widget can be used with the new field type. $widget_info = field_info_widget_types($instance['widget']['type']); if (!in_array($field['type'], $widget_info['field types'])) { // Fallback to using the field type's default widget. $instance['widget']['type'] = $type_info['default_widget']; $widget_info = field_info_widget_types($type_info['default_widget']); $instance['widget']['module'] = $widget_info['module']; $instance['widget']['settings'] = array_intersect_key($instance['widget']['settings'], $widget_info['settings']); $instance['widget']['settings'] += $widget_info['settings']; } // Validate the existing formatters can be used with the new field type. foreach ($instance['display'] as $view_mode => $display) { if ($display['type'] !== 'hidden') { $formatter_info = field_info_formatter_types($display['type']); if (!in_array($field['type'], $formatter_info['field types'])) { // Fallback to using the field type's default formatter. $instance['display'][$view_mode]['type'] = $type_info['default_formatter']; $formatter_info = field_info_formatter_types($type_info['default_formatter']); $instance['display'][$view_mode]['module'] = $formatter_info['module']; $instance['display'][$view_mode]['settings'] = array_intersect_key($instance['display'][$view_mode], $formatter_info['settings']); $instance['display'][$view_mode]['settings'] += $formatter_info['settings']; } } } // Allow anything to be overridden before it gets saved. $instance = drupal_array_merge_deep($instance, $field_instance_overrides); drupal_write_record('field_config_instance', $instance, array('id')); // Clear caches. field_cache_clear(); module_invoke_all('field_update_instance', $instance, $prior_instance); } }