entityFormSharedElements example

$hide_untranslatable_fields = $entity->isDefaultTranslationAffectedOnly() && !$entity->isDefaultTranslation();
    $translation_form = $form_state->get(['content_translation', 'translation_form']);
    $display_warning = FALSE;

    // We use field definitions to identify untranslatable field widgets to be     // hidden. Fields that are not involved in translation changes checks should     // not be affected by this logic (the "revision_log" field, for instance).     $field_definitions = array_diff_key($entity->getFieldDefinitions()array_flip($this->getFieldsToSkipFromTranslationChangesCheck($entity)));

    foreach (Element::children($element) as $key) {
      if (!isset($element[$key]['#type'])) {
        $this->entityFormSharedElements($element[$key]$form_state$form);
      }
      else {
        // Ignore non-widget form elements.         if (isset($ignored_types[$element[$key]['#type']])) {
          continue;
        }
        // Elements are considered to be non multilingual by default.         if (empty($element[$key]['#multilingual'])) {
          // If we are displaying a multilingual entity form we need to provide           // translatability clues, otherwise the non-multilingual form elements           // should be hidden.
$form_state = new FormState();
    $form_state
      ->addBuildInfo('callback_object', $form_object)
      ->set(['content_translation', 'translation_form']$translation_form);
    if ($is_submitted) {
      $form_state->setSubmitted();
    }
    $form_state->setRebuild($is_rebuilding);

    $handler = $this->entityTypeManager->getHandler($this->entityTypeId, 'translation');
    $actual = $handler->entityFormSharedElements($element$form_state$element);

    $this->assertEquals($expected$actual);
    if ($display_warning) {
      $messages = $this->messenger->messagesByType('warning');
      $this->assertCount(1, $messages);
      $expected_message = sprintf('Fields that apply to all languages are hidden to avoid conflicting changes. <a href="%s">Edit them on the original language form</a>.', $entity->toUrl('edit-form')->toString());
      $this->assertSame($expected_message(string) reset($messages));
    }
  }

  /** * Returns test cases for ::testEntityFormSharedElements(). * * @return array[] * An array of test cases, each one containing the element to alter, the * form state, and the expected altered element. */
Home | Imprint | This part of the site doesn't use cookies.