You are here

public static function EntityReferenceLayoutWidget::addTranslatabilityClue in Entity Reference with Layout 8

After-build callback for adding the translatability clue from the widget.

ContentTranslationHandler::addTranslatabilityClue() adds an "(all languages)" suffix to the widget title, replicate that here.

Parameters

array $element: The Form element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The element containing a translatability clue.

File

src/Plugin/Field/FieldWidget/EntityReferenceLayoutWidget.php, line 1451

Class

EntityReferenceLayoutWidget
Entity Reference with Layout field widget.

Namespace

Drupal\entity_reference_layout\Plugin\Field\FieldWidget

Code

public static function addTranslatabilityClue(array $element, FormStateInterface $form_state) {
  static $suffix, $fapi_title_elements;

  // Widgets could have multiple elements with their own titles, so remove the
  // suffix if it exists, do not recurse lower than this to avoid going into
  // nested paragraphs or similar nested field types.
  // Elements which can have a #title attribute according to FAPI Reference.
  if (!isset($suffix)) {
    $suffix = ' <span class="translation-entity-all-languages">(' . t('all languages') . ')</span>';
    $fapi_title_elements = array_flip([
      'checkbox',
      'checkboxes',
      'date',
      'details',
      'fieldset',
      'file',
      'item',
      'password',
      'password_confirm',
      'radio',
      'radios',
      'select',
      'textarea',
      'textfield',
      'weight',
    ]);
  }

  // Update #title attribute for all elements that are allowed to have a
  // #title attribute according to the Form API Reference. The reason for this
  // check is because some elements have a #title attribute even though it is
  // not rendered; for instance, field containers.
  if (isset($element['#type']) && isset($fapi_title_elements[$element['#type']]) && isset($element['#title'])) {
    $element['#title'] .= $suffix;
  }
  elseif ($children = Element::children($element)) {
    foreach ($children as $delta) {
      $element[$delta] = static::addTranslatabilityClue($element[$delta], $form_state);
    }
  }
  elseif (isset($element['#title'])) {
    $element['#title'] .= $suffix;
  }
  return $element;
}