openquestions-1.0.x-dev/src/Form/OqQuestionForm.php
src/Form/OqQuestionForm.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 question entity edit forms.
*/
class OqQuestionForm 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);
$question = $this->getEntity();
if ($question && $question->id() && $question->get('locked_status')->value) {
$form['label']['widget'][0]['value']['#disabled'] = TRUE;
$form['description']['#disabled'] = TRUE;
$form['status']['widget']['#disabled'] = TRUE;
$form['options']['widget']['#disabled'] = TRUE;
$form['uid']['widget']['#disabled'] = TRUE;
$form['created']['widget']['#disabled'] = TRUE;
$form['intro'] = [
'#type' => 'item',
'#markup' => $this->t('This question is locked and cannot be edited. It was locked on @date with the message %msg', [
'@date' => $this->dateFormatter->format($question->get('locked_date')->value),
'%msg' => $question->get('locked_reason')->value,
]),
'#weight' => -1000,
];
}
$description = '<p>' . $this->t('The possible votes on this question. Enter one value per line, in the format <em>key|label|modifier</em>.');
$description .= '<br/>' . $this->t('Only "key" is required and it will be used as the vote value. Currently, the only value for <em>modifier</em> is <em>LABEL</em>.');
$description .= '<br/>' . $this->t('For instance, if you enter <em>1|Yes|LABEL</em> it will be rendered as <em>Yes</em>.');
$description .= '<br/>' . $this->t('If you enter <em>1|Yes</em> it will be rendered as <em>1 (Yes)</em>.');
$description .= '<br/>' . $this->t('If you enter <em>1</em> it will be rendered as <em>1</em>.');
$description .= '<br/>' . $this->t('In all of those cases, it will have a vote value of 1.');
$description .= '</p>';
$form['options']['widget'][0]['value']['#description'] = $description;
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$result = parent::validateForm($form, $form_state);
$entity = $this->buildEntity($form, $form_state);
$value = $form_state->getValue('options');
$value = !empty($value[0]['value']) ? $value[0]['value'] : '';
$ary = $this->oqUtils->parseStringList($value, ['LABEL']);
if (!empty($ary['errors'])) {
$form_state->setErrorByName('options', $this->t('Invalid options: @msgs', ['@msgs' => implode(' ;; ', $ary['errors'])]));
}
return $entity;
}
/**
* {@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 question %label has been created.', $message_arguments));
$this->logger('openquestions')->notice('Created new oq question %label', $logger_arguments);
break;
case SAVED_UPDATED:
$this->messenger()->addStatus($this->t('The oq question %label has been updated.', $message_arguments));
$this->logger('openquestions')->notice('Updated oq question %label.', $logger_arguments);
break;
}
$form_state->setRedirect('entity.oq_question.canonical', ['oq_question' => $entity->id()]);
return $result;
}
}
