Drupal field_settings_form

醉酒当歌 提交于 2020-01-03 03:21:08

问题


I made my own Field in Drupal for an address. It displays things like street, number, zip,... So far so good. But for some reason, ALL field are required. Although there are set required in the UI or the Array.

So I would like to edit it's field_settings_form. I found myself an example in the Drupal-core code, but it doesn't help me a lot. Goal of the field_settings is to make the fields visible or not and required or not. So I came up with this code (I got it from user_reference.module)

function mymodule_field_settings_form($field, $instance, $has_data) {
    $settings = array_keys($field['settings']);
    $form = array();
    $form['required_fields'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Required fields'),
        '#default_value' => is_array($settings['required_fields'])
            ? array('required')
            : array(),
        '#options' => $settings,
    );
    return $form;
}

But I'm quite stuck here. Anyone with some experience in this matter?


回答1:


After a lot of research and even more trial and error, I found this myself. First, I need to fill the "Form Settings" page, so we can check whether or not the page should be visible/required.

function mymodule_field_settings_form($field, $instance, $has_data) {
    $address_fields = array_keys($field['columns']);

    // Get all the address values and put them in an array
    $options = array();
    foreach ($address_fields as $value) {
        $options[$value] = $value;
    }

    // Fill in the values in the dropdown
    $form = array();
    $form['required_fields']['#type'] = 'checkboxes';
    $form['required_fields']['#title'] =  t('Required fields');
    $form['required_fields']['#default_value'] = $field['settings']['required_fields'];
    $form['required_fields']['#options'] = $options;

    $form['visible_fields']['#type'] = 'checkboxes';
    $form['visible_fields']['#title'] =  t('Visible fields');
    $form['visible_fields']['#default_value'] = $field['settings']['visible_fields'];
    $form['visible_fields']['#options'] = $options;

return $form;
}

And in anther function, I stated this. $element calls a function which returns all the elements available. It carries the same values as $options in the function above. If the 'required_field' is on, we make it required, if the 'visible_field' isn't on, we unset it!

  $element = _mymodule_load_element_fields();

    foreach (array_keys($element) as $field_key) {
       $element[$field_key]['#default_value'] = isset($items[$delta][$field_key]) ? $items[$delta][$field_key] : '';        // Set default value
       $element[$field_key]['#required'] = $field['settings']['required_fields'][$field_key] != '0';

    // Set required property
    if ($field['settings']['visible_fields'][$field_key] == '0') {
        unset($element[$field_key]);
    }

I hope this helps you!




回答2:


The problem is that the 'field' in this context is the collection of form elements as a whole, not each individual form element. When a 'field' is required there's no mechanism for specifying particular form elements within that should be required or not.

I haven't found a satisfactory solution to this yet (I've come up against it several times now), the best I've been able to do is to make the field not required and then hook into validation with hook_field_validate and do my 'required' validation there. It's not ideal but it works:

function mymodule_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  if ($field['type'] == 'the_field_type') {

    // Loop through field values
    foreach ($items as $delta => $item) {

      // Validate what you need to against the individual field columns
      if (empty($item['address_line_1'])) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'mymodule_address_error', 
          'message' => 'The first line of the address is required.',
        );
      }

      if (empty($item['town'])) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'mymodule_address_error', 
          'message' => 'The town of the address is required.',
        );
      }

      // And so on...
    }
  }
}


来源:https://stackoverflow.com/questions/8516724/drupal-field-settings-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!