openquestions-1.0.x-dev/src/Form/OqItemForm.php

src/Form/OqItemForm.php
<?php

namespace Drupal\openquestions\Form;

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\openquestions\Service\OQUtilsInterface;

/**
 * Form controller for the oq item entity edit forms.
 */
class OqItemForm extends ContentEntityForm {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Our utils.
   *
   * @var \Drupal\openquestions\Service\OQUtilsInterface
   */
  protected $oqUtils;

  /**
   * Constructs a OqApplicationForm object.
   *
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
   *   Date formatter.
   * @param \Drupal\openquestions\Service\OQUtilsInterface $oqUtils
   *   Our utils.
   */
  public function __construct(EntityRepositoryInterface $entity_repository,
                EntityTypeBundleInfoInterface $entity_type_bundle_info,
                TimeInterface $time,
                DateFormatterInterface $dateFormatter,
                OQUtilsInterface $oqUtils) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->dateFormatter = $dateFormatter;
    $this->oqUtils = $oqUtils;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.repository'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time'),
      $container->get('date.formatter'),
      $container->get('openquestions.oq_utils')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    $item = $this->getEntity();
    if ($item && $item->id() && $item->get('locked_status')->value) {
      $form['label']['widget'][0]['value']['#disabled'] = TRUE;
      $form['description']['#disabled'] = TRUE;
      $form['status']['widget']['#disabled'] = TRUE;
      $form['questions']['widget']['#disabled'] = TRUE;
      $form['uid']['widget']['#disabled'] = TRUE;
      $form['created']['widget']['#disabled'] = TRUE;

      $form['intro'] = [
        '#type' => 'item',
        '#markup' => $this->t('This item is locked and cannot be edited. It was locked on @date with the message %msg', [
          '@date' => $this->dateFormatter->format($item->get('locked_date')->value),
          '%msg' => $item->get('locked_reason')->value,
        ]),
        '#weight' => -1000,
      ];
    }

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $result = parent::save($form, $form_state);

    $entity = $this->getEntity();

    $message_arguments = ['%label' => $entity->toLink()->toString()];
    $logger_arguments = [
      '%label' => $entity->label(),
      'link' => $entity->toLink($this->t('View'))->toString(),
    ];

    switch ($result) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('New oq item %label has been created.', $message_arguments));
        $this->logger('openquestions')->notice('Created new oq item %label', $logger_arguments);
        break;

      case SAVED_UPDATED:
        $this->messenger()->addStatus($this->t('The oq item %label has been updated.', $message_arguments));
        $this->logger('openquestions')->notice('Updated oq item %label.', $logger_arguments);
        break;
    }

    $form_state->setRedirect('entity.oq_item.canonical', ['oq_item' => $entity->id()]);

    return $result;
  }

}

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

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