/** * Override EntityReferenceHandler::settingsForm(). */ public function settingsForm($field, $instance) { $form = parent::settingsForm($field, $instance); $settings = !empty($instance['settings']['behaviors']['og_widget']) ? $instance['settings']['behaviors']['og_widget'] : array(); $settings += array('default' => array('widget_type' => 'options_select', 'widget_settings' => array()), 'admin' => array('widget_type' => 'entityreference_autocomplete', 'widget_settings' => array())); $field_types = array('default' => array('title' => t('Default widget type'), 'description' => t('The widget type of the field as it will appear to the user.')), 'admin' => array('title' => t('Administrator widget type'), 'description' => t('The widget type of the field that will appear only to a user with "Administer group" permission.'))); module_load_include('inc', 'field_ui', 'field_ui.admin'); $widget_types = field_ui_widget_type_options('entityreference'); unset($widget_types['og_complex']); $field_info_widget_types = field_info_widget_types(); foreach ($field_types as $field_type => $value) { $settings_id = 'og-' . $field_type . '-settings'; $form[$field_type]['widget_type'] = array('#type' => 'select', '#title' => $value['title'], '#required' => TRUE, '#options' => $widget_types, '#default_value' => $settings[$field_type]['widget_type'], '#description' => $value['description'], '#ajax' => array('callback' => 'og_' . $field_type . '_settings_form_ajax', 'wrapper' => $settings_id, 'method' => 'replace', 'event' => 'change')); $form[$field_type]['widget_settings'] = array('#type' => 'fieldset', '#title' => t('@title Settings', array('@title' => $value['title'])), '#tree' => TRUE, '#attributes' => array('id' => $settings_id)); if ($settings[$field_type]['widget_type'] && !empty($field_info_widget_types[$settings[$field_type]['widget_type']])) { $widget_type = $field_info_widget_types[$settings[$field_type]['widget_type']]; $current_settings = isset($settings[$field_type]['widget_settings']) ? $settings[$field_type]['widget_settings'] : array(); $fake_instance = array('widget' => array('type' => $settings[$field_type]['widget_type'], 'module' => $widget_type['module'], 'settings' => $current_settings) + $instance['widget']) + $instance; $additions = module_invoke($widget_type['module'], 'field_widget_settings_form', $field, $fake_instance); if ($additions) { $form[$field_type]['widget_settings'] += $additions; } } } // Field access settings. $form['access_override'] = array('#title' => t('Allow entity access to control field access'), '#description' => t('By default, the <em>administer group</em> permission is required to directly edit this field. Selecting this option will allow access to anybody with access to edit the entity.'), '#type' => 'checkbox', '#default_value' => isset($settings['access_override']) ? $settings['access_override'] : FALSE); return $form; }
/** * @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']; }
/** * @Given that the widget for the :field_name field of the :bundle :entity_type is changed to :widget_type_machine_name * * Behat is not very good at filling in form fields that are taxonomy * term trees, or checkboxes. The reason for this is that Behat is * overly energetic about regularly deleting and recreating all of the * test content, sometimes even in the middle of a scenario. The result * of this is that the term ids all change between the time the checkbox * is "clicked", and the time the form is submitted, leading to a validation * error. With this step definition, we can change taxonomy checkbox * fields into autocomplete forms, which Behat fills in with a String value * rather than a term id -- which works consistently. * * Example: * "Given that the widget for the offices field of the regnum entityform is changed to taxonomy_autocomplete" * "And the cache has been cleared" * * http://dropbucket.org/node/1265 */ function change_form_widget($entity_type, $bundle, $field_name, $widget_type_machine_name) { // Retrieve the stored instance settings to merge with the incoming values. $instance = field_read_instance($entity_type, $field_name, $bundle); // Set the right module information. $widget_type = field_info_widget_types($widget_type_machine_name); $widget_module = $widget_type['module']; $instance['widget']['type'] = $widget_type_machine_name; $instance['widget']['module'] = $widget_module; // Update field instance field_update_instance($instance); }
/** * Set widget type and update defaults accordingly. * * @see _field_write_instance(). */ public function setWidget($widget_type_name, $settings = array()) { $this->widget['type'] = $widget_type_name; $this->widget['settings'] = $settings; $widget_type = \field_info_widget_types($widget_type_name); $this->widget['module'] = $widget_type['module']; $this->widget['settings'] += \field_info_widget_settings($widget_type_name); }
public static function getTypes($type = null) { return field_info_widget_types($type); }
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); } }