You are here

class ContentEntity in Commerce Core 8.2

Provides an inline form for managing a content entity.

If the content entity is translatable, and the inline form is used as a part of another content entity form, the entity langcodes will be kept in sync. Translating the parent entity will also translate the entity managed by the inline form.

Plugin annotation


@CommerceInlineForm(
  id = "content_entity",
  label = @Translation("Content entity"),
)

Hierarchy

Expanded class hierarchy of ContentEntity

File

src/Plugin/Commerce/InlineForm/ContentEntity.php, line 23

Namespace

Drupal\commerce\Plugin\Commerce\InlineForm
View source
class ContentEntity extends EntityInlineFormBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'form_mode' => 'default',
      'skip_save' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function requiredConfiguration() {
    return [
      'form_mode',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildInlineForm(array $inline_form, FormStateInterface $form_state) {
    $inline_form = parent::buildInlineForm($inline_form, $form_state);
    assert($this->entity instanceof ContentEntityInterface);
    if ($this->entity
      ->isTranslatable()) {
      $this->entity = $this
        ->ensureTranslation($this->entity, $form_state);
    }
    $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->configuration['form_mode']);
    $form_display
      ->buildForm($this->entity, $inline_form, $form_state);
    return $inline_form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateInlineForm(array &$inline_form, FormStateInterface $form_state) {
    parent::validateInlineForm($inline_form, $form_state);
    assert($this->entity instanceof ContentEntityInterface);
    $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->configuration['form_mode']);
    $form_display
      ->extractFormValues($this->entity, $inline_form, $form_state);
    $form_display
      ->validateFormValues($this->entity, $inline_form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitInlineForm(array &$inline_form, FormStateInterface $form_state) {
    parent::submitInlineForm($inline_form, $form_state);
    assert($this->entity instanceof ContentEntityInterface);
    $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->configuration['form_mode']);
    $form_display
      ->extractFormValues($this->entity, $inline_form, $form_state);
    if ($this->entity
      ->isTranslatable()) {
      $this
        ->updateLangcode($this->entity, $inline_form, $form_state);
    }
    if (empty($this->configuration['skip_save'])) {
      $this->entity
        ->save();
    }
  }

  /**
   * Updates the entity langcode to match the form langcode.
   *
   * Allows the user to select a different language through the langcode
   * form element, which is then transferred to form state.
   *
   * Performed only if the inline form doesn't have a langcode form element
   * of its own.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity.
   * @param array $inline_form
   *   The inline form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  protected function updateLangcode(ContentEntityInterface $entity, array $inline_form, FormStateInterface $form_state) {
    $form_langcode = $form_state
      ->get('langcode');
    if (empty($form_langcode)) {

      // The top-level form is not a content entity form.
      return;
    }
    $langcode_key = $this
      ->getLangcodeKey($entity);

    // The inline form has a visible langcode element, don't override its value.
    if (isset($inline_form[$langcode_key]) && Element::isVisibleElement($inline_form[$langcode_key])) {
      return;
    }
    $entity_langcode = $entity
      ->get($langcode_key)->value;
    if ($entity_langcode != $form_langcode && !$entity
      ->hasTranslation($form_langcode)) {
      $entity
        ->set($langcode_key, $form_langcode);
    }
  }

  /**
   * Ensures the correct entity translation.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return \Drupal\Core\Entity\ContentEntityInterface
   *   The translated entity.
   *
   * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes()
   */
  protected function ensureTranslation(ContentEntityInterface $entity, FormStateInterface $form_state) {
    $form_langcode = $form_state
      ->get('langcode');
    if (empty($form_langcode)) {

      // The top-level form is not a content entity form.
      return $entity;
    }
    $langcode_key = $this
      ->getLangcodeKey($entity);
    $entity_langcode = $entity
      ->get($langcode_key)->value;
    if ($this
      ->isTranslating($form_state) && !$entity
      ->hasTranslation($form_langcode)) {

      // Create a translation from the source language values.
      $source = $form_state
        ->get([
        'content_translation',
        'source',
      ]);
      $source_langcode = $source ? $source
        ->getId() : $entity_langcode;
      if (!$entity
        ->hasTranslation($source_langcode)) {
        $entity
          ->addTranslation($source_langcode, $entity
          ->toArray());
      }
      $source_translation = $entity
        ->getTranslation($source_langcode);
      $entity
        ->addTranslation($form_langcode, $source_translation
        ->toArray());
      $translation = $entity
        ->getTranslation($form_langcode);
      $translation
        ->set('content_translation_source', $source_langcode);

      // Make sure we do not inherit the affected status from the source values.
      if ($entity
        ->getEntityType()
        ->isRevisionable()) {
        $translation
          ->setRevisionTranslationAffected(NULL);
      }
    }
    if ($entity_langcode != $form_langcode && $entity
      ->hasTranslation($form_langcode)) {

      // Switch to the needed translation.
      $entity = $entity
        ->getTranslation($form_langcode);
    }
    return $entity;
  }

  /**
   * Determines whether there's a translation in progress.
   *
   * If the parent entity is being translated, then all inline entities
   * are candidates for translating as well.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return bool
   *   TRUE if translating is in progress, FALSE otherwise.
   *
   * @see \Drupal\Core\Entity\ContentEntityForm::initFormLangcodes()
   */
  protected function isTranslating(FormStateInterface $form_state) {
    $form_langcode = $form_state
      ->get('langcode');
    $default_langcode = $form_state
      ->get('entity_default_langcode');
    return $form_langcode && $form_langcode != $default_langcode;
  }

  /**
   * Gets the langcode key for the given entity.
   *
   * Ensures that the key is not empty, which is allowed by Drupal
   * even if the entity itself is translatable.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity.
   *
   * @return string
   *   The langcode key.
   *
   * @throws \RuntimeException
   *   Thrown when the entity type does not have a langcode key defined.
   */
  protected function getLangcodeKey(ContentEntityInterface $entity) {
    $entity_type = $entity
      ->getEntityType();
    $langcode_key = $entity_type
      ->getKey('langcode');
    if (empty($langcode_key)) {
      throw new \RuntimeException(sprintf('The entity type %s did not specify a langcode key.', $entity_type
        ->id()));
    }
    return $langcode_key;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxFormTrait::ajaxRefreshForm public static function Ajax handler for refreshing an entire form.
ContentEntity::buildInlineForm public function Builds the inline form. Overrides InlineFormBase::buildInlineForm
ContentEntity::defaultConfiguration public function Gets default configuration for this plugin. Overrides InlineFormBase::defaultConfiguration
ContentEntity::ensureTranslation protected function Ensures the correct entity translation.
ContentEntity::getLangcodeKey protected function Gets the langcode key for the given entity.
ContentEntity::isTranslating protected function Determines whether there's a translation in progress.
ContentEntity::requiredConfiguration protected function Gets the required configuration for this plugin. Overrides InlineFormBase::requiredConfiguration
ContentEntity::submitInlineForm public function Submits the inline form. Overrides InlineFormBase::submitInlineForm
ContentEntity::updateLangcode protected function Updates the entity langcode to match the form langcode.
ContentEntity::validateInlineForm public function Validates the inline form. Overrides InlineFormBase::validateInlineForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityInlineFormBase::$entity protected property The entity. 1
EntityInlineFormBase::getEntity public function Gets the entity. Overrides EntityInlineFormInterface::getEntity
EntityInlineFormBase::setEntity public function Sets the entity. Overrides EntityInlineFormInterface::setEntity
InlineFormBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 4
InlineFormBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
InlineFormBase::getLabel public function Gets the inline form label. Overrides InlineFormInterface::getLabel
InlineFormBase::runSubmit public static function Runs the inline form submission.
InlineFormBase::runValidate public static function Runs the inline form validation.
InlineFormBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
InlineFormBase::updatePageTitle public static function Updates the page title based on the inline form's #page_title property.
InlineFormBase::validateConfiguration protected function Validates configuration. 1
InlineFormBase::__construct public function Constructs a new InlineFormBase object. Overrides PluginBase::__construct 4
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.