*/
public static function validateTitleElement(&$element, FormStateInterface $form_state, $form) {
if ($element['uri']['#value'] !== '' && $element['title']['#value'] === '') {
- $element['title']['#required'] = TRUE;
// We expect the field name placeholder value to be wrapped in t() here,
// so it won't be escaped again as it's already marked safe.
- $form_state->setError($element['title'], t('@name field is required.', ['@name' => $element['title']['#title']]));
+ $form_state->setError($element['title'], t('@title field is required if there is @uri input.', ['@title' => $element['title']['#title'], '@uri' => $element['uri']['#title']]));
}
}
'#default_value' => isset($items[$delta]->title) ? $items[$delta]->title : NULL,
'#maxlength' => 255,
'#access' => $this->getFieldSetting('title') != DRUPAL_DISABLED,
+ '#required' => $this->getFieldSetting('title') === DRUPAL_REQUIRED && $element['#required'],
];
// Post-process the title field to make it conditionally required if URL is
// non-empty. Omit the validation on the field edit form, since the field
// settings cannot be saved otherwise.
- if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') == DRUPAL_REQUIRED) {
+ if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') === DRUPAL_REQUIRED) {
$element['#element_validate'][] = [get_called_class(), 'validateTitleElement'];
+
+ if (!$element['title']['#required']) {
+ // Make title required on the front-end when URI filled-in.
+ $field_name = $this->fieldDefinition->getName();
+
+ $parents = $element['#field_parents'];
+ $parents[] = $field_name;
+ $selector = $root = array_shift($parents);
+ if ($parents) {
+ $selector = $root . '[' . implode('][', $parents) . ']';
+ }
+
+ $element['title']['#states']['required'] = [
+ ':input[name="' . $selector . '[' . $delta . '][uri]"]' => ['filled' => TRUE]
+ ];
+ }
}
// Exposing the attributes array in the widget is left for alternate and more
// title of the 'uri' element.
if ($this->getFieldSetting('title') == DRUPAL_DISABLED) {
$element['uri']['#title'] = $element['#title'];
+ // By default the field description is added to the title field. Since
+ // the title field is disabled, we add the description, if given, to the
+ // uri element instead.
+ if (!empty($element['#description'])) {
+ if (empty($element['uri']['#description'])) {
+ $element['uri']['#description'] = $element['#description'];
+ }
+ else {
+ // If we have the description of the type of field together with
+ // the user provided description, we want to make a distinction
+ // between "core help text" and "user entered help text". To make
+ // this distinction more clear, we put them in an unordered list.
+ $element['uri']['#description'] = [
+ '#theme' => 'item_list',
+ '#items' => [
+ // Assume the user-specified description has the most relevance,
+ // so place it first.
+ $element['#description'],
+ $element['uri']['#description'],
+ ],
+ ];
+ }
+ }
}
// Otherwise wrap everything in a details element.
else {