예제 #1
0
 public static function byName($name)
 {
     $class = \get_called_class();
     if ($data = \field_read_field($name)) {
         return new $class($data);
     }
     return FALSE;
 }
예제 #2
0
 /**
  * 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);
     }
 }
예제 #3
0
 /**
  * Change a field's type, even if it has data.
  *
  * @param $field_name
  *   The name of the field to change.
  * @param $type
  *   The type of field to change it to.
  * @param array $column_renames
  *   An array of existing field schema columns to rename. For example, if the
  *   old field type has a column 'value' which maps to the new field type's
  *   'data' column, use array('value' => 'data') to ensure the old column
  *   is just renamed instead of dropped. To ensure an old field column is
  *   dropped, for example, if the same column name is used in the new
  *   field type, but is used to store different data, use
  *   array('old_column' => FALSE).
  * @param array $field_overrides
  *   An optional array that overrides any of the values in the $field
  *   definition array prior to saving.
  * @param array $field_instance_overrides
  *   An optional array that overrides any of the values in any of the field's
  *   instance definition array prior to saving.
  *
  * @return array
  *   The change field if everything was successful.
  *
  * @throws Exception
  */
 public static function changeType($field_name, $type, array $column_renames = array(), array $field_overrides = array(), array $field_instance_overrides = array())
 {
     $field = $prior_field = field_read_field($field_name);
     if (empty($field)) {
         throw new Exception("Field {$field_name} does not exist or is inactive or deleted.");
     }
     if ($field['type'] === $type) {
         throw new Exception("Field {$field_name} is already type {$type}.");
     }
     if ($field['storage']['type'] !== 'field_sql_storage') {
         throw new Exception("Unable to change field type for field {$field_name} using storage {$field['storage']['type']}.");
     }
     $type_info = field_info_field_types($type);
     if (empty($type_info)) {
         throw new Exception("Invalid field type {$type}.");
     }
     $transaction = db_transaction();
     try {
         // Serialize properties back into the data property so it can be saved
         // to the database.
         $field['data'] = array();
         foreach ($field as $key => $value) {
             switch ($key) {
                 case 'id':
                 case 'field_name':
                 case 'type':
                 case 'module':
                 case 'active':
                 case 'locked':
                 case 'cardinality':
                 case 'deleted':
                 case 'data':
                     break;
                 default:
                     $field['data'][$key] =& $field[$key];
             }
         }
         // Update basic information on the field config.
         $field['type'] = $type;
         $field['module'] = $type_info['module'];
         $field['settings'] = array_intersect_key($field['settings'], $type_info['settings']);
         $field['settings'] += $type_info['settings'];
         // @todo Check if $field['translatable'] needs to be changed.
         // Make any final field overrides before updating the schema and saving
         // the field config record back to the database.
         $field = drupal_array_merge_deep($field, $field_overrides);
         static::changeSchema($field, $column_renames);
         drupal_write_record('field_config', $field, array('id'));
         // Now update the instances for this field.
         static::changeInstances($field, $field_instance_overrides);
         // Clear caches
         field_cache_clear();
         // Invoke external hooks after the cache is cleared for API consistency.
         $has_data = field_has_data($field);
         module_invoke_all('field_update_field', $field, $prior_field, $has_data);
         watchdog('helper', "Converted field {$field_name} from {$prior_field['type']} to {$type}.");
         return $field;
     } catch (Exception $e) {
         $transaction->rollback();
         watchdog_exception('helper', $e);
         throw $e;
     }
 }
예제 #4
0
 /**
  * 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;
 }