You are here

protected function EntityTranslationUnifiedFormInlineMode::addTranslatabilityClue in Entity Translation Unified Form 8

Adds a clue about the form element translatability.

If the given element does not have a #title attribute, the function is recursively applied to child elements.

Parameters

array $element: A form element array.

This was taken and modified from content_translation

1 call to EntityTranslationUnifiedFormInlineMode::addTranslatabilityClue()
EntityTranslationUnifiedFormInlineMode::alterTitle in src/Plugin/EntityTranslationUnifiedFormMode/EntityTranslationUnifiedFormInlineMode.php
Helper function to add the language label to a title.

File

src/Plugin/EntityTranslationUnifiedFormMode/EntityTranslationUnifiedFormInlineMode.php, line 69

Class

EntityTranslationUnifiedFormInlineMode
@EntityTranslationUnifiedFormMode ( id = "EntityTranslationUnifiedFormInlineMode", admin_label = Plugin annotation @Translation("Inline Mode"), )

Namespace

Drupal\entity_translation_unified_form\Plugin\EntityTranslationUnifiedFormMode

Code

protected function addTranslatabilityClue(&$element, $suffix) {
  static $fapi_title_elements;
  $done = true;

  // Elements which can have a #title attribute according to FAPI Reference.
  if (!isset($fapi_title_elements)) {
    $fapi_title_elements = array_flip([
      'checkbox',
      'checkboxes',
      'date',
      'details',
      'fieldset',
      'file',
      'item',
      'password',
      'password_confirm',
      'radio',
      'radios',
      'select',
      'text_format',
      '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;

    // If we changed a invisible title the job is not done.
    if (isset($element['#title_display']) && $element['#title_display'] == 'invisible') {
      return false;
    }
    else {
      return true;
    }
  }

  // If the current element does not have a (valid) title, try child elements.
  if ($children = Element::children($element)) {
    foreach ($children as $delta) {
      if (!$this
        ->addTranslatabilityClue($element[$delta], $suffix)) {
        $done = false;
      }
    }
  }

  // If children are not "really" changed, apply to parent (happens with fields with cardinality>1).
  if (!$done && isset($element['#title'])) {
    $element['#title'] .= $suffix;
  }
}