Skip to main content
added 46 characters in body
Source Link
Marc
  • 137
  • 2
  • 6

Based on @DavidCzinege answer, a version that works for any form where the field appears and also for multivalued fields:

use Drupal\Core\Render\Element;

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {

  if (!empty($form['field_file']['widget'])) {

    foreach (Element::children($form['field_file']['widget']) as $el_index) {

      $form['field_file']['widget'][$el_index]['#process'][] = 'my_module_set_file_description_required';

  }

}

function my_module_set_file_description_required($element) {

        $element['description']['#required'] = TRUE;
        return $element;

      };

    }

  }

}

Based on @DavidCzinege answer, a version that works for any form where the field appears and also for multivalued fields:

use Drupal\Core\Render\Element;

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {

  if (!empty($form['field_file']['widget'])) {

    foreach (Element::children($form['field_file']['widget']) as $el_index) {

      $form['field_file']['widget'][$el_index]['#process'][] = function ($element) {

        $element['description']['#required'] = TRUE;
        return $element;

      };

    }

  }

}

Based on @DavidCzinege answer, a version that works for any form where the field appears and also for multivalued fields:

use Drupal\Core\Render\Element;

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {

  if (!empty($form['field_file']['widget'])) {

    foreach (Element::children($form['field_file']['widget']) as $el_index) {

      $form['field_file']['widget'][$el_index]['#process'][] = 'my_module_set_file_description_required';

  }

}

function my_module_set_file_description_required($element) {

  $element['description']['#required'] = TRUE;
  return $element;

};
Source Link
Marc
  • 137
  • 2
  • 6

Based on @DavidCzinege answer, a version that works for any form where the field appears and also for multivalued fields:

use Drupal\Core\Render\Element;

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {

  if (!empty($form['field_file']['widget'])) {

    foreach (Element::children($form['field_file']['widget']) as $el_index) {

      $form['field_file']['widget'][$el_index]['#process'][] = function ($element) {

        $element['description']['#required'] = TRUE;
        return $element;

      };

    }

  }

}