You are here

class LingotekMetadataEditForm in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  2. 4.0.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  3. 3.0.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  4. 3.1.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  5. 3.2.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  6. 3.3.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  7. 3.4.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  8. 3.5.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  9. 3.6.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  10. 3.7.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm
  11. 3.8.x src/Form/LingotekMetadataEditForm.php \Drupal\lingotek\Form\LingotekMetadataEditForm

Hierarchy

Expanded class hierarchy of LingotekMetadataEditForm

File

src/Form/LingotekMetadataEditForm.php, line 21

Namespace

Drupal\lingotek\Form
View source
class LingotekMetadataEditForm extends ContentEntityForm {
  use LingotekSetupTrait;

  /**
   * The language-locale mapper.
   *
   * @var \Drupal\lingotek\LanguageLocaleMapperInterface
   */
  protected $languageLocaleMapper;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The Lingotek content translation service.
   *
   * @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service
   */
  protected $translationService;

  /**
   * The entity type id.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * Constructs a new LingotekManagementForm object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\lingotek\LanguageLocaleMapperInterface $language_locale_mapper
   *  The language-locale mapper.
   * @param \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service
   *   The Lingotek content translation service.
   * @param string $entity_type_id
   *   The entity type id.
   */
  public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, LanguageLocaleMapperInterface $language_locale_mapper, LingotekContentTranslationServiceInterface $translation_service, ModuleHandlerInterface $module_handler, $entity_type_id) {
    parent::__construct($entity_manager);
    $this->languageManager = $language_manager;
    $this->languageLocaleMapper = $language_locale_mapper;
    $this->translationService = $translation_service;
    $this->entityTypeId = $entity_type_id;
    $this
      ->setModuleHandler($module_handler);
    $this->lingotek = \Drupal::service('lingotek');
    $this->operation = 'lingotek_metadata';
    $entity = $this
      ->getEntityFromRouteMatch($this
      ->getRouteMatch(), $this->entityTypeId);
    $this
      ->setEntity($entity);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.manager'), $container
      ->get('language_manager'), $container
      ->get('lingotek.language_locale_mapper'), $container
      ->get('lingotek.content_translation'), $container
      ->get('module_handler'), \Drupal::routeMatch()
      ->getParameter('entity_type_id'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    if ($redirect = $this
      ->checkSetup()) {
      return $redirect;
    }

    // $form = parent::buildForm($form, $form_state);
    $entity = $this
      ->getEntity();
    $lingotek_document_id = $this->translationService
      ->getDocumentId($entity);
    $source_status = $this->translationService
      ->getSourceStatus($entity);
    $form['metadata']['notice'] = [
      '#markup' => $this
        ->t('Editing the metadata manually can cause diverse errors. If you find yourself using it often, please contact the module maintainers because you may have hit a bug.'),
      '#prefix' => '<span class="warning">',
      '#suffix' => '</span',
    ];
    $form['metadata']['lingotek_document_id'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Lingotek Document ID'),
      '#default_value' => $lingotek_document_id,
    ];
    $form['metadata']['lingotek_source_status'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Lingotek Source Status'),
      '#default_value' => $source_status,
      '#options' => $this
        ->getLingotekStatusesOptions(),
    ];
    $languages = $this->languageManager
      ->getLanguages();
    foreach ($languages as $langcode => $language) {
      $form['metadata']['lingotek_target_status'][$langcode] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Lingotek Target Status: ') . $language
          ->getName(),
        '#default_value' => $this->translationService
          ->getTargetStatus($entity, $langcode),
        '#options' => $this
          ->getLingotekStatusesOptions(),
      ];
    }
    $form['actions'] = [];
    $form['actions']['save_metadata'] = array(
      '#type' => 'submit',
      '#value' => t('Save metadata'),
      '#button_type' => 'primary',
      '#limit_validation_errors' => array(),
      '#submit' => [
        [
          $this,
          'saveMetadata',
        ],
      ],
    );
    return $form;
  }

  /**
   * Submit handler that saves the metadata of this content entity.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function saveMetadata(array &$form, FormStateInterface $form_state) {
    $entity = $this
      ->getEntity();
    $input = $form_state
      ->getUserInput();
    $lingotek_document_id = $input['lingotek_document_id'];
    $source_status = $input['lingotek_source_status'];

    // We need to delete the key before inserting it in the table.
    \Drupal::database()
      ->delete('lingotek_content_metadata')
      ->condition('document_id', $lingotek_document_id)
      ->condition('entity_type', $entity
      ->getEntityTypeId())
      ->condition('entity_id', $entity
      ->id())
      ->execute();
    $this->translationService
      ->setDocumentId($entity, $lingotek_document_id);
    $this->translationService
      ->setSourceStatus($entity, $source_status);
    foreach ($this->languageManager
      ->getLanguages() as $langcode => $language) {
      $this->translationService
        ->setTargetStatus($entity, $langcode, $input[$langcode]);
    }
    drupal_set_message($this
      ->t('Metadata saved successfully'));
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseFormId() {
    return 'lingotek_metadata_entity_form';
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    $form_id = $this->entityTypeId;
    $form_id .= '_' . $this
      ->getBaseFormId();
    return $form_id;
  }
  public function getLingotekStatusesOptions() {
    return [
      Lingotek::STATUS_CURRENT => $this
        ->t('Current'),
      Lingotek::STATUS_EDITED => $this
        ->t('Edited'),
      Lingotek::STATUS_IMPORTING => $this
        ->t('Importing'),
      Lingotek::STATUS_PENDING => $this
        ->t('Pending'),
      Lingotek::STATUS_READY => $this
        ->t('Ready'),
      Lingotek::STATUS_REQUEST => $this
        ->t('Request'),
      Lingotek::STATUS_UNTRACKED => $this
        ->t('Untracked'),
    ];
  }

  /**
   * Form submission handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Not needed, we have our own handler.
  }
  protected function getEditedFieldNames(FormStateInterface $form_state) {
    return [];
  }
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {

    // Do nothing. We don't want to alter the entity.
  }
  protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {

    // ToDo
    // We ignore violations.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityForm::$entity protected property The entity being used by this form. Overrides EntityForm::$entity 9
ContentEntityForm::$entityRepository protected property The entity repository service.
ContentEntityForm::$entityTypeBundleInfo protected property The entity type bundle info service.
ContentEntityForm::$time protected property The time service.
ContentEntityForm::addRevisionableFormFields protected function Add revision form fields if the entity enabled the UI.
ContentEntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityForm::buildEntity 3
ContentEntityForm::form public function Gets the actual form array to be built. Overrides EntityForm::form 13
ContentEntityForm::getBundleEntity protected function Returns the bundle entity of the entity, or NULL if there is none.
ContentEntityForm::getFormDisplay public function Gets the form display. Overrides ContentEntityFormInterface::getFormDisplay
ContentEntityForm::getFormLangcode public function Gets the code identifying the active form language. Overrides ContentEntityFormInterface::getFormLangcode
ContentEntityForm::getNewRevisionDefault protected function Should new revisions created on default.
ContentEntityForm::init protected function Initializes the form state and the entity before the first form build. Overrides EntityForm::init 1
ContentEntityForm::initFormLangcodes protected function Initializes form language code values.
ContentEntityForm::isDefaultFormLangcode public function Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface::isDefaultFormLangcode
ContentEntityForm::prepareEntity protected function Prepares the entity object before the form is built first. Overrides EntityForm::prepareEntity 1
ContentEntityForm::setFormDisplay public function Sets the form display. Overrides ContentEntityFormInterface::setFormDisplay
ContentEntityForm::showRevisionUi protected function Checks whether the revision form fields should be added to the form.
ContentEntityForm::updateChangedTime public function Updates the changed time of the entity.
ContentEntityForm::updateFormLangcode public function Updates the form language to reflect any change to the entity language.
ContentEntityForm::validateForm public function Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level… Overrides FormBase::validateForm 3
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
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actions protected function Returns an array of supported actions for the current entity form. 29
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::save public function Form submission handler for the 'save' action. Overrides EntityFormInterface::save 41
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LingotekMetadataEditForm::$entityTypeId protected property The entity type id.
LingotekMetadataEditForm::$languageLocaleMapper protected property The language-locale mapper.
LingotekMetadataEditForm::$languageManager protected property The language manager.
LingotekMetadataEditForm::$translationService protected property The Lingotek content translation service.
LingotekMetadataEditForm::buildForm public function Form constructor. Overrides EntityForm::buildForm
LingotekMetadataEditForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties Overrides ContentEntityForm::copyFormValuesToEntity
LingotekMetadataEditForm::create public static function Instantiates a new instance of this class. Overrides ContentEntityForm::create
LingotekMetadataEditForm::flagViolations protected function Flags violations for the current form. Overrides ContentEntityForm::flagViolations
LingotekMetadataEditForm::getBaseFormId public function Returns a string identifying the base form. Overrides EntityForm::getBaseFormId
LingotekMetadataEditForm::getEditedFieldNames protected function Gets the names of all fields edited in the form. Overrides ContentEntityForm::getEditedFieldNames
LingotekMetadataEditForm::getFormId public function Returns a unique string identifying the form. Overrides EntityForm::getFormId
LingotekMetadataEditForm::getLingotekStatusesOptions public function
LingotekMetadataEditForm::saveMetadata public function Submit handler that saves the metadata of this content entity.
LingotekMetadataEditForm::submitForm public function Form submission handler. Overrides ContentEntityForm::submitForm
LingotekMetadataEditForm::__construct public function Constructs a new LingotekManagementForm object. Overrides ContentEntityForm::__construct
LingotekSetupTrait::$lingotek protected property A lingotek connector object
LingotekSetupTrait::checkSetup protected function Verify the Lingotek Translation module has been properly initialized.
LingotekSetupTrait::setupCompleted public function Checks if Lingotek module is completely set up.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.