public function build() { $type = self::guessType($this->type); if (!($info = field_info_field($this->name))) { field_create_field(array('field_name' => $this->name, 'type' => $type, 'module' => '')); } }
/** * Creates a date field from an array of settings values. * * All values have defaults, only need to specify values that need to be * different. */ protected function createDateField($values = array()) { extract($values); $field_name = !empty($field_name) ? $field_name : 'field_test'; $entity_type = !empty($entity_type) ? $entity_type : 'node'; $bundle = !empty($bundle) ? $bundle : 'story'; $label = !empty($label) ? $label : 'Test'; $field_type = !empty($field_type) ? $field_type : 'datetime'; $repeat = !empty($repeat) ? $repeat : 0; $todate = !empty($todate) ? $todate : 'optional'; $widget_type = !empty($widget_type) ? $widget_type : 'date_select'; $tz_handling = !empty($tz_handing) ? $tz_handling : 'site'; $granularity = !empty($granularity) ? $granularity : array('year', 'month', 'day', 'hour', 'minute'); $year_range = !empty($year_range) ? $year_range : '2010:+1'; $input_format = !empty($input_format) ? $input_format : variable_get('date_format_html_date', 'Y-m-d') . ' ' . variable_get('date_format_html_time', 'H:i:s'); $input_format_custom = !empty($input_format_custom) ? $input_format_custom : ''; $text_parts = !empty($text_parts) ? $text_parts : array(); $increment = !empty($increment) ? $increment : 15; $default_value = !empty($default_value) ? $default_value : 'now'; $default_value2 = !empty($default_value2) ? $default_value2 : 'blank'; $default_format = !empty($default_format) ? $default_format : 'long'; $cache_enabled = !empty($cache_enabled); $cache_count = !empty($cache_count) ? $cache_count : 4; $field = array('field_name' => $field_name, 'type' => $field_type, 'cardinality' => !empty($repeat) ? FIELD_CARDINALITY_UNLIMITED : 1, 'settings' => array('granularity' => $granularity, 'tz_handling' => $tz_handling, 'timezone_db' => date_get_timezone_db($tz_handling), 'repeat' => $repeat, 'todate' => $todate, 'cache_enabled' => $cache_enabled, 'cache_count' => $cache_count)); $instance = array('entity_type' => $entity_type, 'field_name' => $field_name, 'label' => $label, 'bundle' => $bundle, 'weight' => -4, 'widget' => array('type' => $widget_type, 'settings' => array('increment' => $increment, 'year_range' => $year_range, 'input_format' => $input_format, 'input_format_custom' => $input_format_custom, 'text_parts' => $text_parts, 'label_position' => 'above'), 'weight' => -4), 'settings' => array('default_value' => $default_value, 'default_value2' => $default_value2)); $instance['display'] = array('default' => array('label' => 'above', 'type' => 'date_default', 'settings' => array('format_type' => $default_format, 'show_repeat_rule' => 'show', 'multiple_number' => '', 'multiple_from' => '', 'multiple_to' => '', 'fromto' => 'both'), 'module' => 'date', 'weight' => 0), 'teaser' => array('label' => 'above', 'type' => 'date_default', 'weight' => 0, 'settings' => array('format_type' => $default_format, 'show_repeat_rule' => 'show', 'multiple_number' => '', 'multiple_from' => '', 'multiple_to' => '', 'fromto' => 'both'), 'module' => 'date')); $field = field_create_field($field); $instance = field_create_instance($instance); field_info_cache_clear(TRUE); field_cache_clear(TRUE); // Look at how the field got configured. $this->drupalGet("admin/structure/types/manage/{$bundle}/fields/{$field_name}"); $this->drupalGet("admin/structure/types/manage/{$bundle}/display"); }
/** * Create a new file field. * * @param $name * The name of the new field (all lowercase), exclude the "field_" prefix. * @param $type_name * The node type that this field will be added to. * @param $field_settings * A list of field settings that will be added to the defaults. * @param $instance_settings * A list of instance settings that will be added to the instance defaults. * @param $widget_settings * A list of widget settings that will be added to the widget defaults. */ function createFileField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) { $field = array('field_name' => $name, 'type' => 'file', 'settings' => array(), 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1); $field['settings'] = array_merge($field['settings'], $field_settings); field_create_field($field); $this->attachFileField($name, 'node', $type_name, $instance_settings, $widget_settings); }
public static function installFields(array $fields) { foreach ($fields as $index => $field) { if (field_info_field($field['field_name'])) { continue; } $fields[$index] = field_create_field($field); } return $fields; }
public function setUp() { parent::setUp(array('collection', 'collection_field', 'xautoload', 'entity')); $field = array('active' => '1', 'cardinality' => '-1', 'deleted' => '0', 'entity_types' => array(), 'field_name' => 'field_collection', 'foreign keys' => array(), 'indexes' => array('collection' => array(0 => 'value')), 'locked' => '0', 'module' => 'collection_field', 'settings' => array(), 'translatable' => '0', 'type' => 'collection'); field_create_field($field); $instance = array('bundle' => 'article', 'entity_type' => 'node', 'field_name' => 'field_collection', 'label' => 'Collection', 'widget' => array('module' => 'collections', 'type' => 'collection'), 'display' => array('default' => array('label' => 'hidden', 'settings' => array(), 'type' => 'hidden'), 'teaser' => array('label' => 'hidden', 'settings' => array(), 'type' => 'hidden'))); field_create_instance($instance); $this->createCollection(); $this->node = $this->createNode(); }
/** * 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; }
/** * Utility method to create field_instance fields. * * @param int $cardinality * Cardinality of field to create. */ protected function createFields($cardinality = 4) { // Create a field_collection field to use for the tests. $this->fieldName = 'field_test_collection'; $this->field = array('field_name' => $this->fieldName, 'type' => 'field_collection', 'cardinality' => $cardinality); $this->field = field_create_field($this->field); $this->fieldId = $this->field['id']; $this->instance = array('field_name' => $this->fieldName, 'entity_type' => 'node', 'bundle' => 'article', 'label' => $this->randomName() . '_label', 'description' => $this->randomName() . '_description', 'weight' => mt_rand(0, 127), 'settings' => array(), 'widget' => array('type' => 'hidden', 'label' => 'Test', 'settings' => array())); $this->instance = field_create_instance($this->instance); // Add a field to the collection. $field = array('field_name' => 'field_text', 'type' => 'text', 'cardinality' => 1, 'translatable' => FALSE); field_create_field($field); $instance = array('entity_type' => 'field_collection_item', 'field_name' => 'field_text', 'bundle' => $this->fieldName, 'label' => 'Test text field', 'widget' => array('type' => 'text_textfield')); field_create_instance($instance); }
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); } }
/** * Tests the 'link_separate' formatter. * * This test is mostly the same as testLinkFormatter(), but they cannot be * merged, since they involve different configuration and output. */ function testLinkSeparateFormatter() { // Create a field with settings to validate. $this->field = array( 'field_name' => drupal_strtolower($this->randomName()), 'type' => 'link', 'cardinality' => 2, ); field_create_field($this->field); $this->instance = array( 'field_name' => $this->field['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', 'settings' => array( 'title' => DRUPAL_OPTIONAL, ), 'widget' => array( 'type' => 'link_default', ), 'display' => array( 'full' => array( 'type' => 'link_separate', 'label' => 'hidden', ), ), ); field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; // Create an entity with two link field values: // - The first field item uses a URL only. // - The second field item uses a URL and title. // For consistency in assertion code below, the URL is assigned to the title // variable for the first field. $this->drupalGet('test-entity/add/test_bundle'); $url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com'; $url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org'; // Intentionally contains an ampersand that needs sanitization on output. $title2 = 'A very long & strange example title that could break the nice layout of the site'; $edit = array( "{$this->field['field_name']}[$langcode][0][url]" => $url1, "{$this->field['field_name']}[$langcode][1][url]" => $url2, "{$this->field['field_name']}[$langcode][1][title]" => $title2, ); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); $id = $match[1]; $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id))); // Verify that the link is output according to the formatter settings. $options = array( 'trim_length' => array(NULL, 6), 'rel' => array(NULL, 'nofollow'), 'target' => array(NULL, '_blank'), ); foreach ($options as $setting => $values) { foreach ($values as $new_value) { // Update the field formatter settings. $this->instance['display']['full']['settings'] = array($setting => $new_value); field_update_instance($this->instance); $this->renderTestEntity($id); switch ($setting) { case 'trim_length': $url = $url1; $url_title = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url; $expected = '<div class="link-item">'; $expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_title) . '</a></div>'; $expected .= '</div>'; $this->assertRaw($expected); $url = $url2; $url_title = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url; $title = isset($new_value) ? truncate_utf8($title2, $new_value, FALSE, TRUE) : $title2; $expected = '<div class="link-item">'; $expected .= '<div class="link-title">' . check_plain($title) . '</div>'; $expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_title) . '</a></div>'; $expected .= '</div>'; $this->assertRaw($expected); break; case 'rel': $rel = isset($new_value) ? ' rel="' . $new_value . '"' : ''; $this->assertRaw('<div class="link-url"><a href="' . check_plain($url1) . '"' . $rel . '>' . check_plain($url1) . '</a></div>'); $this->assertRaw('<div class="link-url"><a href="' . check_plain($url2) . '"' . $rel . '>' . check_plain($url2) . '</a></div>'); break; case 'target': $target = isset($new_value) ? ' target="' . $new_value . '"' : ''; $this->assertRaw('<div class="link-url"><a href="' . check_plain($url1) . '"' . $target . '>' . check_plain($url1) . '</a></div>'); $this->assertRaw('<div class="link-url"><a href="' . check_plain($url2) . '"' . $target . '>' . check_plain($url2) . '</a></div>'); break; } } } }
/** * 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()); }
/** * Create field instance using constructed instance array. * * @return array * Field array as returned by Field API CRUD operations. */ public function save() { return field_create_field($this->field); }
/** * keep user entity fields function for ldap_user * in base class instead of user test class in case * module integration testing is needed */ function createTestUserFields() { foreach ($this->ldap_user_test_entity_fields() as $field_id => $field_conf) { $field_info = field_info_field($field_id); if (!$field_info) { field_create_field($field_conf['field']); field_create_instance($field_conf['instance']); } $field_info = field_info_field($field_id); } }
/** * Utility function to create new user entity fields. */ protected function webformUserAddField($name, $label, $type) { $field = array('field_name' => $name, 'type' => $type, 'cardinality' => 1); field_create_field($field); $instance = array('field_name' => $name, 'label' => $label, 'entity_type' => 'user', 'bundle' => 'user'); field_create_instance($instance); }
/** * Adds availability reference field to a content type. * * @When /^I add the "(?<field_name>[^"]*)" availability reference field referencing to "(?<unit_types>[^"]*)" units in "(?<content_type>[^"]*)" content$/ */ public function iAddTheAvailabilityReferenceFieldReferencingToUnitsInPageContent($field_name, $unit_types, $content_type) { // Create the content type. // Make sure a testimonial content type doesn't already exist. if (!in_array($content_type, node_type_get_names())) { $type = array('type' => $content_type, 'name' => $content_type, 'base' => 'node_content', 'custom' => 1, 'modified' => 1, 'locked' => 0); $type = node_type_set_defaults($type); node_type_save($type); node_add_body_field($type); $this->content_types[] = $content_type; } // Create field ('rooms_booking_unit_options') if not exist. if (field_read_field($field_name) === FALSE) { $field = array('field_name' => $field_name, 'type' => 'rooms_availability_reference', 'cardinality' => -1, 'settings' => array('referenceable_unit_types' => drupal_map_assoc(explode(',', $unit_types)))); field_create_field($field); $this->fields[] = $field_name; } if (field_read_instance('node', $field_name, $content_type) === FALSE) { // Create the instance on the bundle. $instance = array('field_name' => $field_name, 'entity_type' => 'node', 'label' => 'Availability reference', 'bundle' => $content_type, 'required' => FALSE, 'widget' => array('type' => 'rooms_availability_reference_autocomplete')); field_create_instance($instance); } }
/** * TODO write documentation. * @param $type * @param $field_name * @param $settings * @return unknown_type */ protected function drupalCreateField($type, $field_name = NULL, $settings = array()) { if (!isset($field_name)) { $field_name = strtolower($this->randomName()); } $field_definition = array('field_name' => $field_name, 'type' => $type); $field_definition += $settings; field_create_field($field_definition); $field = field_read_field($field_name); $this->assertTrue($field, t('Created field @field_name of type @type.', array('@field_name' => $field_name, '@type' => $type))); return $field; }
/** * Create a field in a group and in a vocabulary. * * @param string $field_name * Name of the field. * @param string $field_type * Type of the field. * @param string $group_name * Name of the group. * @param string $vocabulary_name * Name of the vocabulary. * * @Given the field :field_type named :field_name grouped in :group_name in the vocabulary :vocabulary_name exists * * @Then I create a new field :field_type named :field_name grouped in :group_name in the vocabulary :vocabulary_name */ public function iCreateNewFieldNamedGroupedInInTheVocabulary($field_name, $field_type, $group_name, $vocabulary_name) { $field_machine_name = $this->transliterate->getMachineName('field_' . $field_name); $field_machine_type = $this->getFieldTypeFormatByName($field_type); $group_machine_name = $this->transliterate->getMachineName('group_' . $group_name); $vocabulary_machine_name = $this->transliterate->getMachineName($vocabulary_name); // Make sure the field doesn't already exist. if (!field_info_field($field_machine_name)) { // Create a field. $field = array('field_name' => $field_machine_name, 'type' => $field_machine_type, 'label' => $field_name); field_create_field($field); // Attach the field to our taxonomy entity. $instance = array('field_name' => $field_machine_name, 'entity_type' => 'taxonomy_term', 'bundle' => $vocabulary_machine_name, 'label' => $field_name, 'description' => ''); field_create_instance($instance); // Backup fields created. $this->fields[] = $field; $groups = field_group_read_groups(array('name' => 'taxonomy_term', 'bundle' => $vocabulary_machine_name, 'view_mode' => 'full')); $your_group = $groups['taxonomy_term'][$vocabulary_machine_name]['form'][$group_machine_name]; $your_group->children[] = $field_machine_name; field_group_group_save($your_group); } else { throw new \InvalidArgumentException("The field '{$field_name}' already exists."); } }