taxonomy_menu_sync-1.0.4/src/Form/TaxonomyMenuSyncUiForm.php

src/Form/TaxonomyMenuSyncUiForm.php
<?php

namespace Drupal\taxonomy_menu_sync\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\taxonomy_menu_sync\Entity\TaxonomyMenuSync;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Sync menu configurations.
 */
class TaxonomyMenuSyncUiForm extends ConfirmFormBase {

  /**
   * ID of the item to delete.
   *
   * @var int
   */
  protected $id;

  /**
   * Entity object.
   */
  protected TaxonomyMenuSync $entity;

  /**
   * @var \Drupal\taxonomy_menu_sync\TaxonomyMenuSyncHelper
   */
  protected $taxonomyMenuSyncHelper;

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->taxonomyMenuSyncHelper = $container->get('taxonomy_menu_sync.helper');
    return $instance;
  }

  /**
   * {@inheritDoc}
   */
  public function getFormId() : string {
    return 'taxonomy_menu_sync_sync_form';
  }

  /**
   *
   */
  public function getConfirmText() {
    return $this->t('Synchronize');
  }

  /**
   * {@inheritDoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to sync %menu?', ['%menu' => $this->entity->label()]);
  }

  /**
   * {@inheritDoc}
   */
  public function getCancelUrl() {
    return new Url('entity.taxonomy_menu_sync.collection');
  }

  /**
   * {@inheritDoc}
   */
  public function getDescription() {
    $message = $this->t('<b>Warning!!! You are about to synchronize the taxonomy into menu. This action cannot be undone.</b>');
    if ($this->entity->isSynced()) {
      $message = $this->t('<b>Warning!!! This taxonomy menu sync has been already synchronize. Be careful while re-sync. This action cannot be undone.</b>');
    }
    return $message;
  }

  /**
   * {@inheritDoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, string $taxonomy_menu_sync = NULL) {
    $this->id = $taxonomy_menu_sync;
    if (!empty($this->id) && ($entity = $this->taxonomyMenuSyncHelper->entityLoad('taxonomy_menu_sync', $this->id)) instanceof TaxonomyMenuSync) {
      /** @var \Drupal\taxonomy_menu_sync\Entity\TaxonomyMenuSync $entity */
      $this->entity = $entity;
    }
    else {
      throw new NotFoundHttpException();
    }

    if ($this->entity->isSynced()) {
      $form['force_sync'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Force Sync menu'),
        '#required' => TRUE,
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritDoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    if ($this->entity->isSynced() && !$form_state->getValue('force_sync')) {
      $form_state->setErrorByName('force_sync', $this->t('Force sync menu field is required.'));
    }
  }

  /**
   * {@inheritDoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->taxonomyMenuSyncHelper->sync($this->entity->id());
    $this->messenger()->addMessage($this->t('Taxonomy menu sync %menu has been synchronized.', ['%menu' => $this->entity->label()]));
    $form_state->setRedirect('entity.taxonomy_menu_sync.collection');
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc