Drupal field_settings_form

守給你的承諾、 提交于 2019-12-06 14:50:19

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!

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