Problem/Motivation

The description's text in the description field in the file field cannot be changed.

description

This is because at the fileWidget.php we have this:

 // Add the description field if enabled.
    if ($element['#description_field'] && $item['fids']) {
      $config = \Drupal::config('file.settings');
      $element['description'] = array(
        '#type' => $config->get('description.type'),
        '#title' => t('Description'),
        '#value' => isset($item['description']) ? $item['description'] : '',
        '#maxlength' => $config->get('description.length'),
        '#description' => t('The description may be used as the label of the link to the file.'),
      );
    }

Which basically overwrite any possible value in the description form.

Proposed resolution

Before to just overwrite the $element['description'] let's check if it already exists and if that is the case then merge the values.

Remaining tasks

  1. Write patch
  2. Write a test

User interface changes

None

API changes

None

Data model changes

None

CommentFileSizeAuthor
description-description.png21.16 KBgnuget
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gnuget created an issue. See original summary.

gnuget’s picture

Category: Bug report » Support request
Status: Active » Closed (works as designed)

Ok, After to work on this for a few hours I found how to do it.

basically, the description field is build in a #process callback so we need to add another callback. something like this:


use Drupal\Core\Render\Element;

/**
 * Implements hook_form_alter().
 */
function HOOK_form_alter(
    &$form,
    \Drupal\Core\Form\FormStateInterface $form_state,
    $form_id
) {

    foreach (Element::children($form['FIELD']['widget']) as $key) {
      $form['FIELD']['widget'][$key]['#process'][] = 'change_description';
    }
}

function change_description($element, \Drupal\Core\Form\FormState $form_state, $form) {
  $element['description']['#description'] = 'The new description';
  return $element;
}