closedquestion-8.x-3.x-dev/src/Form/ClosedQuestionAnswerForm.php
src/Form/ClosedQuestionAnswerForm.php
<?php
namespace Drupal\closedquestion\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form handler for the answers edit forms of closed questions.
*
* @internal
*/
class ClosedQuestionAnswerForm extends ContentEntityForm {
/**
* The Current User object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a form object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @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\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user, DateFormatterInterface $date_formatter) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->currentUser = $current_user;
$this->dateFormatter = $date_formatter;
}
/**
* {@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('current_user'),
$container->get('date.formatter')
);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\closedquestion\Entity\ClosedQuestionAnswerInterface $entity */
$entity = $this->entity;
if ($this->operation == 'edit') {
$form['#title'] = $this->t('<em>Edit</em> @title', [
'@title' => $entity->label(),
]);
}
// Changed must be sent to the client, for later overwrite error checking.
$form['changed'] = [
'#type' => 'hidden',
'#default_value' => $entity->getChangedTime(),
];
$form = parent::form($form, $form_state);
$form['status']['#group'] = 'footer';
// Closed question author information for administrators.
$form['author'] = [
'#type' => 'details',
'#title' => t('Authoring information'),
'#group' => 'advanced',
'#attributes' => [
'class' => ['closedquestion-form-author'],
],
'#attached' => [
'library' => ['closedquestion/drupal.closedquestion'],
],
'#weight' => 90,
'#optional' => TRUE,
];
if (isset($form['uid'])) {
$form['uid']['#group'] = 'author';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'author';
}
$form['#attached']['library'][] = 'closedquestion/closedquestion.form';
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$entity = $this->entity;
$element['delete']['#access'] = $entity->access('delete');
$element['delete']['#weight'] = 100;
return $element;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$insert = $entity->isNew();
$entity->save();
$entity_link = $entity->toLink($this->t('View'))->toString();
$context = ['%title' => $entity->label(), 'link' => $entity_link];
$t_args = ['%title' => $entity->toLink()->toString()];
if ($insert) {
$this->logger('closedquestion')->notice('The answer %title has been created.', $context);
$this->messenger()->addStatus($this->t('The answer %title has been created.', $t_args));
}
else {
$this->logger('closedquestion')->notice('The answer %title has been updated.', $context);
$this->messenger()->addStatus($this->t('The answer %title has been updated.', $t_args));
}
if ($entity->id()) {
$form_state->setValue('id', $entity->id());
$form_state->set('id', $entity->id());
$form_state->setRedirect('entity.closedquestion_answer.collection');
}
else {
// In the unlikely case something went wrong on save, the entity will be
// rebuilt and entity form redisplayed the same way as in preview.
$this->messenger()->addError($this->t('The answer could not be saved.'));
$form_state->setRebuild();
}
}
}
