Example #1
0
 /**
  * Returns the element properties for this element.
  *
  * @return array
  *   An array of element properties. See
  *   \Drupal\Core\Render\ElementInfoManagerInterface::getInfo() for
  *   documentation of the standard properties of all elements, and the
  *   return value format.
  */
 public function getInfo()
 {
     $parts = _name_translations();
     $field_settings = \Drupal::service('plugin.manager.field.field_type')->getDefaultStorageSettings('name');
     $instance_settings = \Drupal::service('plugin.manager.field.field_type')->getDefaultFieldSettings('name');
     return array('#input' => TRUE, '#process' => array('name_element_expand'), '#pre_render' => array('name_element_pre_render'), '#element_validate' => array('name_element_validate'), '#theme_wrappers' => array('form_element'), '#show_component_required_marker' => 0, '#default_value' => array('title' => '', 'given' => '', 'middle' => '', 'family' => '', 'generational' => '', 'credentials' => ''), '#minimum_components' => $field_settings['minimum_components'], '#allow_family_or_given' => $field_settings['allow_family_or_given'], '#components' => array('title' => array('type' => $instance_settings['field_type']['title'], 'title' => $parts['title'], 'title_display' => 'description', 'inline_css' => $instance_settings['inline_css']['title'], 'size' => $instance_settings['size']['title'], 'maxlength' => $field_settings['max_length']['title'], 'options' => $field_settings['title_options'], 'autocomplete' => FALSE), 'given' => array('type' => 'textfield', 'title' => $parts['given'], 'title_display' => 'description', 'inline_css' => $instance_settings['inline_css']['given'], 'size' => $instance_settings['size']['given'], 'maxlength' => $field_settings['max_length']['given'], 'autocomplete' => FALSE), 'middle' => array('type' => 'textfield', 'title' => $parts['middle'], 'title_display' => 'description', 'inline_css' => $instance_settings['inline_css']['middle'], 'size' => $instance_settings['size']['middle'], 'maxlength' => $field_settings['max_length']['middle'], 'autocomplete' => FALSE), 'family' => array('type' => 'textfield', 'title' => $parts['family'], 'title_display' => 'description', 'inline_css' => $instance_settings['inline_css']['family'], 'size' => $instance_settings['size']['family'], 'maxlength' => $field_settings['max_length']['family'], 'autocomplete' => FALSE), 'generational' => array('type' => $instance_settings['field_type']['generational'], 'title' => $parts['generational'], 'title_display' => 'description', 'inline_css' => $instance_settings['inline_css']['generational'], 'size' => $instance_settings['size']['generational'], 'maxlength' => $field_settings['max_length']['generational'], 'options' => $field_settings['generational_options'], 'autocomplete' => FALSE), 'credentials' => array('type' => 'textfield', 'title' => $parts['credentials'], 'title_display' => 'description', 'inline_css' => $instance_settings['inline_css']['credentials'], 'size' => $instance_settings['size']['credentials'], 'maxlength' => $field_settings['max_length']['credentials'], 'autocomplete' => FALSE)));
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function settingsSummary()
 {
     $settings = $this->getSettings();
     $summary = array();
     $field_name = explode('.', $this->fieldDefinition->id());
     $field_name = end($field_name);
     $machine_name = isset($settings['format']) ? $settings['format'] : 'default';
     $name_format = entity_load('name_format', $machine_name);
     if ($name_format) {
         $summary[] = t('Format: %format (@machine_name)', array('%format' => $name_format->label(), '@machine_name' => $name_format->id()));
     } else {
         $summary[] = t('Format: <strong>Missing format.</strong><br/>This field will be displayed using the Default format.');
         $machine_name = 'default';
     }
     // Provide an example of the selected format.
     module_load_include('admin.inc', 'name');
     $used_components = $this->getFieldSetting('components');
     $excluded_components = array_diff_key($used_components, _name_translations());
     $examples = name_example_names($excluded_components, $field_name);
     if ($examples && ($example = array_shift($examples))) {
         $format = name_get_format_by_machine_name($machine_name);
         $formatted = SafeMarkup::checkPlain(NameFormatParser::parse($example, $format));
         if (empty($formatted)) {
             $formatted = '<em>&lt;&lt;empty&gt;&gt;</em>';
         }
         $summary[] = t('Example: !example', array('!example' => $formatted));
     }
     $summary[] = t('Markup: @yesno', array('@yesno' => empty($settings['markup']) ? t('no') : t('yes')));
     $output_options = _name_formatter_output_options();
     $output = empty($settings['output']) ? 'default' : $settings['output'];
     $summary[] = t('Output: @format', array('@format' => $output_options[$output]));
     return $summary;
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     module_load_include('inc', 'name', 'includes/name.content');
     $field_settings = $this->getFieldSettings();
     $instance['label'] = 'instance label';
     $element += array('#type' => 'name', '#title' => SafeMarkup::checkPlain($instance['label']), '#label' => $instance['label'], '#components' => array(), '#minimum_components' => array_filter($field_settings['minimum_components']), '#allow_family_or_given' => !empty($field_settings['allow_family_or_given']), '#default_value' => isset($items[$delta]) ? $items[$delta]->getValue() : NULL, '#field' => $this, '#credentials_inline' => empty($field_settings['credentials_inline']) ? 0 : 1, '#component_css' => empty($field_settings['component_css']) ? '' : $field_settings['component_css'], '#component_layout' => empty($field_settings['component_layout']) ? 'default' : $field_settings['component_layout'], '#show_component_required_marker' => !empty($field_settings['show_component_required_marker']));
     $components = array_filter($field_settings['components']);
     foreach (_name_translations() as $key => $title) {
         if (isset($components[$key])) {
             $element['#components'][$key]['type'] = 'textfield';
             $size = !empty($field_settings['size'][$key]) ? $field_settings['size'][$key] : 60;
             $title_display = isset($field_settings['title_display'][$key]) ? $field_settings['title_display'][$key] : 'description';
             $element['#components'][$key]['title'] = SafeMarkup::checkPlain($field_settings['labels'][$key]);
             $element['#components'][$key]['title_display'] = $title_display;
             $element['#components'][$key]['size'] = $size;
             $element['#components'][$key]['maxlength'] = !empty($field_settings['max_length'][$key]) ? $field_settings['max_length'][$key] : 255;
             // Provides backwards compatibility with Drupal 6 modules.
             $field_type = $key == 'title' || $key == 'generational' ? 'select' : 'text';
             $field_type = isset($field_settings['field_type'][$key]) ? $field_settings['field_type'][$key] : (isset($field_settings[$key . '_field']) ? $field_settings[$key . '_field'] : $field_type);
             if ($field_type == 'select') {
                 $element['#components'][$key]['type'] = 'select';
                 $element['#components'][$key]['size'] = 1;
                 $element['#components'][$key]['options'] = $this->optionsProvider->getOptions($this->fieldDefinition, $key);
             } elseif ($field_type == 'autocomplete') {
                 if ($sources = $field_settings['autocomplete_source'][$key]) {
                     $sources = array_filter($sources);
                     if (!empty($sources)) {
                         $element['#components'][$key]['autocomplete'] = array('#autocomplete_route_name' => 'name.autocomplete', '#autocomplete_route_parameters' => array('field_name' => $this->fieldDefinition->getName(), 'entity_type' => $this->fieldDefinition->getTargetEntityTypeId(), 'bundle' => $this->fieldDefinition->getTargetBundle(), 'component' => $key));
                     }
                 }
             }
             if (isset($field_settings['inline_css'][$key]) && Unicode::strlen($field_settings['inline_css'][$key])) {
                 $element['#components'][$key]['attributes'] = array('style' => $field_settings['inline_css'][$key]);
             }
         } else {
             $element['#components'][$key]['exclude'] = TRUE;
         }
     }
     return $element;
 }
Example #4
0
 public static function validateMinimumComponents($element, FormStateInterface $form_state)
 {
     $minimum_components = $form_state->getValue(['settings', 'minimum_components']);
     $diff = array_intersect(array_keys(array_filter($minimum_components)), array('given', 'family'));
     if (count($diff) == 0) {
         $components = array_intersect_key(_name_translations(), array_flip(array('given', 'family')));
         $form_state->setError($element, t('%label must have one of the following components: %components', array('%label' => t('Minimum components'), '%components' => implode(', ', $components))));
     }
     $components = $form_state->getValue(['settings', 'components']);
     $minimum_components = $form_state->getValue(['settings', 'minimum_components']);
     $diff = array_diff_key(array_filter($minimum_components), array_filter($components));
     if (count($diff)) {
         $components = array_intersect_key(_name_translations(), $diff);
         $form_state->setError($element, t('%components can not be selected for %label when they are not selected for %label2.', array('%label' => t('Minimum components'), '%label2' => t('Components'), '%components' => implode(', ', $components))));
     }
 }
Example #5
0
 /**
  * The most basic test. This should only fail if there is a change to the
  * Drupal API.
  */
 function testFieldEntry()
 {
     $this->drupalLogin($this->admin_user);
     $new_name_field = array('label' => 'Test name', 'field_name' => 'name_test', 'new_storage_type' => 'name');
     $this->drupalPostForm('admin/structure/types/manage/page/fields/add-field', $new_name_field, t('Save and continue'));
     $this->resetAll();
     // Required test.
     $field_settings = array();
     foreach ($this->name_getFieldStorageSettings() as $key => $value) {
         $field_settings[$key] = '';
     }
     foreach ($this->name_getFieldStorageSettingsCheckboxes() as $key => $value) {
         $field_settings[$key] = FALSE;
     }
     $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.field_name_test/storage', $field_settings, t('Save field settings'));
     $n = _name_translations();
     $required_messages = array(t('Label for @field field is required.', array('@field' => $n['title'])), t('Label for @field field is required.', array('@field' => $n['given'])), t('Label for @field field is required.', array('@field' => $n['middle'])), t('Label for @field field is required.', array('@field' => $n['family'])), t('Label for @field field is required.', array('@field' => $n['generational'])), t('Label for @field field is required.', array('@field' => $n['credentials'])), t('Maximum length for @field field is required.', array('@field' => $n['title'])), t('Maximum length for @field field is required.', array('@field' => $n['given'])), t('Maximum length for @field field is required.', array('@field' => $n['middle'])), t('Maximum length for @field field is required.', array('@field' => $n['family'])), t('Maximum length for @field field is required.', array('@field' => $n['generational'])), t('Maximum length for @field field is required.', array('@field' => $n['credentials'])), t('@field options are required.', array('@field' => $n['title'])), t('@field options are required.', array('@field' => $n['generational'])), t('@field field is required.', array('@field' => t('Components'))), t('@field must have one of the following components: @components', array('@field' => t('Minimum components'), '@components' => SafeMarkup::checkPlain(implode(', ', array($n['given'], $n['family']))))));
     foreach ($required_messages as $message) {
         $this->assertText($message);
     }
     $field_settings = array('settings[components][title]' => FALSE, 'settings[components][given]' => TRUE, 'settings[components][middle]' => FALSE, 'settings[components][family]' => TRUE, 'settings[components][generational]' => FALSE, 'settings[components][credentials]' => FALSE, 'settings[minimum_components][title]' => TRUE, 'settings[minimum_components][given]' => FALSE, 'settings[minimum_components][middle]' => FALSE, 'settings[minimum_components][family]' => FALSE, 'settings[minimum_components][generational]' => TRUE, 'settings[minimum_components][credentials]' => TRUE, 'settings[max_length][title]' => 0, 'settings[max_length][given]' => -456, 'settings[max_length][middle]' => 'asdf', 'settings[max_length][family]' => 3454, 'settings[max_length][generational]' => 4.5, 'settings[max_length][credentials]' => 'NULL', 'settings[title_options]' => "-- --\nMr.\nMrs.\nMiss\nMs.\nDr.\nProf.\n[vocabulary:machine]", 'settings[generational_options]' => "-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\nVII\nVIII\nIX\nX\n[vocabulary:123]");
     $this->resetAll();
     $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.field_name_test/storage', $field_settings, t('Save field settings'));
     $required_messages = array(t('@field must be higher than or equal to 1.', array('@field' => $n['title'])), t('@field must be higher than or equal to 1.', array('@field' => $n['given'])), t('@field must be a number.', array('@field' => $n['middle'])), t('@field must be lower than or equal to 255.', array('@field' => $n['family'])), t('@field is not a valid number.', array('@field' => $n['generational'])), t('@field must be a number.', array('@field' => $n['credentials'])), t('@field must have one of the following components: @components', array('@field' => t('Minimum components'), '@components' => SafeMarkup::checkPlain(implode(', ', array($n['given'], $n['family']))))), t("The vocabulary 'machine' in @field could not be found.", array('@field' => t('@title options', array('@title' => $n['title'])))), t("The vocabulary '123' in @field could not be found.", array('@field' => t('@generational options', array('@generational' => $n['generational'])))));
     foreach ($required_messages as $message) {
         $this->assertText($message);
     }
     // Make sure option lengths do not exceed the title lengths
     $field_settings = array('settings[max_length][title]' => 5, 'settings[max_length][generational]' => 3, 'settings[title_options]' => "Aaaaa.\n-- --\nMr.\nMrs.\nBbbbbbbb\nMiss\nMs.\nDr.\nProf.\nCcccc.", 'settings[generational_options]' => "AAAA\n-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\nVII\nVIII\nIX\nX\nBBBB");
     $this->resetAll();
     $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.field_name_test/storage', $field_settings, t('Save field settings'));
     $required_messages = array(t('The following options exceed the maximum allowed @field length: Aaaaa., Bbbbbbbb, Ccccc.', array('@field' => t('@title options', array('@title' => $n['title'])))), t('The following options exceed the maximum allowed @field length: AAAA, VIII, BBBB', array('@field' => t('@generational options', array('@generational' => $n['generational'])))));
     foreach ($required_messages as $message) {
         $this->assertText($message);
     }
     // Make sure option have at least one valid option.
     $field_settings = array('settings[title_options]' => " \n-- --\n ", 'settings[generational_options]' => " \n-- --\n ");
     $this->resetAll();
     $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.field_name_test/storage', $field_settings, t('Save field settings'));
     $required_messages = array(t('@field are required.', array('@field' => t('@title options', array('@title' => $n['title'])))), t('@field are required.', array('@field' => t('@generational options', array('@generational' => $n['generational'])))));
     foreach ($required_messages as $message) {
         $this->assertText($message);
     }
     // Make sure option have at least one valid only have one default value.
     $field_settings = array('settings[title_options]' => "-- --\nMr.\nMrs.\nMiss\n-- Bob\nDr.\nProf.", 'settings[generational_options]' => "-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\n--");
     $this->resetAll();
     $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.field_name_test/storage', $field_settings, t('Save field settings'));
     $required_messages = array(t('@field can only have one blank value assigned to it.', array('@field' => t('@title options', array('@title' => $n['title'])))), t('@field can only have one blank value assigned to it.', array('@field' => t('@generational options', array('@generational' => $n['generational'])))));
     foreach ($required_messages as $message) {
         $this->assertText($message);
     }
     // Save the field again with the default values
     $this->resetAll();
     $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.field_name_test/storage', $this->name_getFieldStorageSettings(), t('Save field settings'));
     $this->assertText(t('Updated field Test name field settings.'));
     // Now the widget settings...
     // First, check that field validation is working... cut n paste from above test
     $field_settings = array('settings[components][title]' => FALSE, 'settings[components][given]' => TRUE, 'settings[components][middle]' => FALSE, 'settings[components][family]' => TRUE, 'settings[components][generational]' => FALSE, 'settings[components][credentials]' => FALSE, 'settings[minimum_components][title]' => TRUE, 'settings[minimum_components][given]' => FALSE, 'settings[minimum_components][middle]' => FALSE, 'settings[minimum_components][family]' => FALSE, 'settings[minimum_components][generational]' => TRUE, 'settings[minimum_components][credentials]' => TRUE, 'settings[max_length][title]' => 0, 'settings[max_length][given]' => -456, 'settings[max_length][middle]' => 'asdf', 'settings[max_length][family]' => 3454, 'settings[max_length][generational]' => 4.5, 'settings[max_length][credentials]' => 'NULL', 'settings[title_options]' => "-- --\nMr.\nMrs.\nMiss\nMs.\nDr.\nProf.\n[vocabulary:machine]", 'settings[generational_options]' => "-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\nVII\nVIII\nIX\nX\n[vocabulary:123]");
     $this->resetAll();
     $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.field_name_test/storage', $field_settings, t('Save field settings'));
     $required_messages = array(t('Maximum length for @field must be higher than or equal to 1.', array('@field' => $n['title'])), t('Maximum length for @field must be higher than or equal to 1.', array('@field' => $n['given'])), t('Maximum length for @field must be a number.', array('@field' => $n['middle'])), t('Maximum length for @field must be lower than or equal to 255.', array('@field' => $n['family'])), t('Maximum length for @field is not a valid number.', array('@field' => $n['generational'])), t('Maximum length for @field must be a number.', array('@field' => $n['credentials'])), t('@field must have one of the following components: @components', array('@field' => t('Minimum components'), '@components' => SafeMarkup::checkPlain(implode(', ', array($n['given'], $n['family']))))), t("The vocabulary 'machine' in @field could not be found.", array('@field' => t('@title options', array('@title' => $n['title'])))), t("The vocabulary '123' in @field could not be found.", array('@field' => t('@generational options', array('@generational' => $n['generational'])))));
     foreach ($required_messages as $message) {
         $this->assertText($message);
     }
     $widget_settings = array('settings[title_display][title]' => 'description', 'settings[title_display][given]' => 'description', 'settings[title_display][middle]' => 'description', 'settings[title_display][family]' => 'description', 'settings[title_display][generational]' => 'description', 'settings[title_display][credentials]' => 'description', 'settings[size][title]' => 6, 'settings[size][given]' => 20, 'settings[size][middle]' => 20, 'settings[size][family]' => 20, 'settings[size][generational]' => 5, 'settings[size][credentials]' => 35, 'settings[inline_css][title]' => '', 'settings[inline_css][given]' => '', 'settings[inline_css][middle]' => '', 'settings[inline_css][family]' => '', 'settings[inline_css][generational]' => '', 'settings[inline_css][credentials]' => '');
     $this->resetAll();
     $this->drupalGet('admin/structure/types/manage/page/fields/node.page.field_name_test');
     foreach ($widget_settings as $name => $value) {
         $this->assertFieldByName($name, $value);
     }
 }