/** * Attach a file field to an entity. * * @param $name * The name of the new field (all lowercase), exclude the "field_" prefix. * @param $entity_type * The entity type this field will be added to. * @param $bundle * The bundle 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 attachFileField($name, $entity_type, $bundle, $instance_settings = array(), $widget_settings = array()) { $instance = array('field_name' => $name, 'label' => $name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'required' => !empty($instance_settings['required']), 'settings' => array(), 'widget' => array('type' => 'file_generic', 'settings' => array())); $instance['settings'] = array_merge($instance['settings'], $instance_settings); $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings); field_create_instance($instance); }
/** * 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"); }
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(); }
/** * 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); }
/** * Save field instance to database. * * @see \field_update_instance(). * @see \field_create_instance(). */ public function save() { if (isset($this->id)) { \field_update_instance($this->export()); } else { foreach (\field_create_instance($this->export()) as $k => $v) { $this->{$k} = $v; } } return $this; }
/** * 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; } } } }
/** * Create field instance using constructed instance array. * * @return array * Field array as returned by Field API CRUD operations. */ public function save() { return field_create_instance($this->instance); }
public static function changeInstanceField(array $instance, $new_field_name) { $old_field = field_info_field($instance['field_name']); $new_field = field_info_field($new_field_name); if ($old_field['type'] != $new_field['type']) { throw new FieldException("Cannot change field instance because they are not the same field type."); } if ($old_field['storage']['type'] !== 'field_sql_storage') { throw new FieldException("Unable to change field type for field {$old_field['field_name']} using storage {$old_field['storage']['type']}."); } if ($new_field['storage']['type'] !== 'field_sql_storage') { throw new FieldException("Unable to change field type for field {$new_field['field_name']} using storage {$new_field['storage']['type']}."); } if (!field_info_instance($instance['entity_type'], $new_field_name, $instance['bundle'])) { $new_instance = $instance; $new_instance['field_name'] = $new_field_name; field_create_instance($new_instance); watchdog('helper', "Created new field instance: {$instance['entity_type']}.{$instance['bundle']}.{$new_field_name}"); } // Copy data from old field tables to the new field tables. $old_data_table = _field_sql_storage_tablename($old_field); $new_data_table = _field_sql_storage_tablename($new_field); $query = db_select($old_data_table, 'old'); $query->fields('old'); $query->condition('entity_type', $instance['entity_type']); $query->condition('bundle', $instance['bundle']); db_insert($new_data_table)->from($query)->execute(); $old_revision_table = _field_sql_storage_revision_tablename($old_field); if (db_table_exists($old_revision_table)) { $new_revision_table = _field_sql_storage_revision_tablename($new_field); $query = db_select($old_revision_table, 'old'); $query->fields('old'); $query->condition('entity_type', $instance['entity_type']); $query->condition('bundle', $instance['bundle']); db_insert($new_revision_table)->from($query)->execute(); } FieldHelper::deleteInstance($instance); }
/** * 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 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."); } }
/** * 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 $field_name * @param $widget_type * @param $display_type * @param $bundle * @return unknown_type */ protected function drupalCreateFieldInstance($field_name, $widget_type, $formatter_type, $bundle) { $instance_definition = array('field_name' => $field_name, 'bundle' => $bundle, 'widget' => array('type' => $widget_type), 'display' => array('full' => array('type' => $formatter_type))); field_create_instance($instance_definition); $instance = field_read_instance($field_name, $bundle); $this->assertTrue($instance, t('Created instance of field @field_name on bundle @bundle.', array('@field_name' => $field_name, '@bundle' => $bundle))); return $instance; }
public function build($entityType, $bundleName) { if (!($info = field_info_instance($entityType, $this->field_name, $bundleName))) { field_create_instance(array('entity_type' => $entityType, 'bundle' => $bundleName, 'field_name' => $this->field_name, 'label' => $this->label, 'description' => $this->description, 'required' => $this->required, 'settings' => array(), 'widget' => $this->widget->getDefinition(), 'display' => array())); } }
function BraftonArticleImporter() { $errors = new BraftonErrorReport(variable_get('brafton_api_key'), variable_get('brafton_api_root'), (bool) variable_get('brafton_debug_mode')); //Gathers feed type, Api and Video Keys, and archive file information from the Brafton module settings page. $import_list = array(); $feed_type = variable_get('brafton_feed_type'); $is_api = variable_get('brafton_api_key'); $is_archive = variable_get('brafton_archive_file'); $overwrite = variable_get('brafton_overwrite'); $is_published = variable_get('brafton_published'); $counter = 0; //Define default fields for b_news type_url_form_media if ($is_api || $is_archive) { //Loads the date and overwrite settings. $date = get_date_setting(); //Loads the article objects from the feed into an array. $article_array = load_article_array($is_archive); //Loops through the article array foreach ($article_array as $value) { //Checks to see if the article already exists. If it does not, a new node is created of type b_news. If it does, then depending upon the overwrite settings the existing node is either loaded, or we iterate to the next article in the feed $id = $value->getId(); $check = check_if_article_exists($id); if (!empty($check) && $overwrite == 1) { $nid = key($check['node']); $node = node_load($nid); } elseif (empty($check)) { $node = new stdClass(); } else { continue; } //Gets an array of image information from the feed. $image = get_image_attributes($value); //Gets the article categories as an array of valid and unique term ids. $categories = set_article_categories($value, 'b_news'); //Instantiation of each article component as a field in the node object. $node->status = $is_published == 1 ? 0 : 1; $types = array('body' => 'body', 'image' => 'field_brafton_image', 'tax' => 'field_brafton_term'); if (variable_get('brafton_existing_type') != 'b_news') { $types['body'] = variable_get('brafton_custom_body'); $types['image'] = variable_get('brafton_custom_image'); $types['tax'] = variable_get('brafton_custom_taxonomy'); } $node->type = variable_get('brafton_existing_type'); $node->language = LANGUAGE_NONE; $node->title = $value->getHeadline(); $import_list['title'][] = $node->title; $node->uid = checkAuthor(variable_get('brafton_author'), $value->getByLine()); //$nodestatus = 1; $node->created = strtotime($value->{$date}()); $node->updated = $node->created; $node->promote = 0; $node->sticky = 0; $node->comment = variable_get('brafton_comments'); $node->{$types['body']}[$node->language][0] = array('value' => $value->getText(), 'summary' => $value->getExtract(), 'format' => 'full_html'); if ($image) { $node->{$types['image']}[$node->language][0] = (array) system_retrieve_file($image['url'], NULL, TRUE, FILE_EXISTS_REPLACE); $node->{$types['image']}[$node->language][0]['alt'] = $image['alt']; $node->{$types['image']}[$node->language][0]['title'] = $image['title']; } if (field_info_instance('node', 'field_brafton_id', $node->type) == NULL) { $brafton_id_field = array('field_name' => 'field_brafton_id', 'entity_type' => 'node', 'bundle' => $node->type, 'display' => array('default' => array('label' => 'hidden', 'type' => 'hidden'))); field_create_instance($brafton_id_field); } $node->field_brafton_id[$node->language][0]['value'] = $id; //Setting the article pause cta text //ensure categories don't get added twice $cats = false; $oldcats; if ($overwrite && isset($node->{$types['tax']}[$node->language])) { $oldcats = $node->{$types['tax']}[$node->language]; $cats = true; } foreach ($categories as $category) { if ($cats) { foreach ($oldcats as $oldcat) { if ($oldcat['tid'] != $category) { $node->{$types['tax']}[$node->language][]['tid'] = $category; } } } else { $node->{$types['tax']}[$node->language][]['tid'] = $category; } } //end category code node_save($node); var_dump($node); taxonomy_node_insert($node); $nid = $node->nid; $alias = drupal_get_path_alias("node/" . $nid); $counter++; ++$errors->level; } } $import_list['counter'] = $counter; return $import_list; }
public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false) { /* @var $node FieldInstanceNode */ $entityType = $node->getEntityType(); $bundle = $node->getBundle(); $fieldName = $node->getFieldName(); $existing = field_info_instance($entityType, $fieldName, $bundle); $field = field_info_field($fieldName); $default = array('entity_type' => $entityType, 'bundle' => $bundle, 'field_name' => $fieldName); $object = $node->getValue(); if (!is_array($object)) { $object = array(); } if (empty($object['label'])) { // Field data 'label' key is not part of the Drupal signature // but this module will inject it anyway and should be persisted // along the field 'data' key in database if (empty($field['label'])) { if ($label = $this->findFieldLabel($fieldName)) { $default['label'] = $label; } } else { $default['label'] = $field['label']; } } // This is a forced default from this module: never display new // fields without being explicitely told to $instance = $default + $object + array('display' => array('default' => array('type' => 'hidden'))); // Dynamically determine weight using position relative to parent node // @todo Find a quicker way if ($node->hasParent()) { $instance['weight'] = $node->getParent()->getChildPosition($node); } // Propagate defaults set at the field level if (!empty($field['instance'])) { foreach ($field['instance'] as $key => $value) { if (!isset($instance[$key])) { $instance[$key] = $value; } } } // Even thought this is not mandatory few modules such as the 'image' // module will attempt to access this attribute, without carying about // the field_update_instance() method documentation if (!isset($instance['settings'])) { $instance['settings'] = array(); } // Deal with widget if (!isset($instance['widget'])) { if (isset($field['widget'])) { $instance['widget'] = $field['widget']; } } if ($existing) { $this->alter(self::HOOK_UPDATE, $node, $instance); field_update_instance($instance); } else { $this->alter(self::HOOK_INSERT, $node, $instance); field_create_instance($instance); } }