getFormLangcode example

parent::entityFormAlter($form$form_state$entity);

    if (isset($form['content_translation'])) {
      // We do not need to show these values on node forms: they inherit the       // basic node property values.       $form['content_translation']['status']['#access'] = FALSE;
      $form['content_translation']['name']['#access'] = FALSE;
      $form['content_translation']['created']['#access'] = FALSE;
    }

    $form_object = $form_state->getFormObject();
    $form_langcode = $form_object->getFormLangcode($form_state);
    $translations = $entity->getTranslationLanguages();
    $status_translatable = NULL;
    // Change the submit button labels if there was a status field they affect     // in which case their publishing / unpublishing may or may not apply     // to all translations.     if (!$entity->isNew() && (!isset($translations[$form_langcode]) || count($translations) > 1)) {
      foreach ($entity->getFieldDefinitions() as $property_name => $definition) {
        if ($property_name == 'status') {
          $status_translatable = $definition->isTranslatable();
        }
      }
      
return FALSE;
  }

  /** * {@inheritdoc} */
  public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */

    $metadata = $this->manager->getTranslationMetadata($entity);
    $form_object = $form_state->getFormObject();
    $form_langcode = $form_object->getFormLangcode($form_state);
    $entity_langcode = $entity->getUntranslated()->language()->getId();

    $new_translation = $entity->isNewTranslation();
    $translations = $entity->getTranslationLanguages();
    if ($new_translation) {
      // Make sure a new translation does not appear as existing yet.       unset($translations[$form_langcode]);
    }
    $is_translation = $new_translation || ($entity->language()->getId() != $entity_langcode);
    $has_translations = count($translations) > 1;

    
/** * Initializes the form state and the entity before the first form build. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
  protected function init(FormStateInterface $form_state) {
    // Ensure we act on the translation object corresponding to the current form     // language.     $this->initFormLangcodes($form_state);
    $langcode = $this->getFormLangcode($form_state);
    $this->entity = $this->entity->hasTranslation($langcode) ? $this->entity->getTranslation($langcode) : $this->entity->addTranslation($langcode);

    $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation());
    $this->setFormDisplay($form_display$form_state);

    parent::init($form_state);
  }

  /** * Initializes form language code values. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
Home | Imprint | This part of the site doesn't use cookies.