How to properly alter a field description in hook form alter I am altering a field description (help text) based on certain conditions. However, sometimes the description element to alter is set at $element['value']['#description'] = $new_description; And other times it's set at $element['#description'] = $new_description; I am doing this in a field widget alter hook:

/**
 * Implements hook_field_widget_form_alter().
 */
function my_module_field_widget_form_alter(&amp;$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
    // do the following based on some conditions: 
    $html = \Drupal::service('renderer')-&gt;render($some_render_array]);
    /* @var Drupal\Core\Field\FieldFilteredMarkup $new_description */
    $new_description = \Drupal\Core\Field\FieldFilteredMarkup::create(t($element['#description'] . $html));
    // For some elements this works:
    $element['#description'] = $new_description;
    // For other I need to do this:
    $element['value']['#description'] = $new_description;
  }

}


Whats the difference? and do I have to use some logic to target the appropriate element?
The difference is that $element['value']['#description'] is the description for the property value. If you have a field with multiple properties you can set a description for each property. This is the common practice, but a field widget doesn't have to do it this way. Check formElement() of the widget to see how the element keys are used. Most widgets copy the content of $element to the subkey value, but not all.