knowledge-8.x-1.x-dev/src/Form/WaveForm.php
src/Form/WaveForm.php
<?php
declare(strict_types=1);
namespace Drupal\knowledge\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for the wave entity edit forms.
*/
final class WaveForm extends ContentEntityForm {
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs a ContentEntityForm 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\State\StateInterface $state
* The state service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, StateInterface $state) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->state = $state;
}
/**
* {@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('state')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form = parent::buildForm($form, $form_state);
$form['default'] = [
'#type' => 'checkbox',
'#title' => $this->t('Default'),
'#default_value' => $this->entity->id() == $this->state->get('knowledge.wave_default', 1),
'#description' => $this->t('Set this wave as the default wave.'),
'#weight' => 10,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state): int {
$result = parent::save($form, $form_state);
if ($form_state->getValue('default')) {
$default_wave = $this->state->get('knowledge.wave_default', 1);
if ($this->entity->id() != $default_wave) {
$this->state->set('knowledge.wave_default', $this->entity->id());
}
}
$message_args = ['%label' => $this->entity->toLink()->toString()];
$logger_args = [
'%label' => $this->entity->label(),
'link' => $this->entity->toLink($this->t('View'))->toString(),
];
switch ($result) {
case SAVED_NEW:
$this->messenger()->addStatus($this->t('New wave %label has been created.', $message_args));
$this->logger('knowledge')->notice('New wave %label has been created.', $logger_args);
break;
case SAVED_UPDATED:
$this->messenger()->addStatus($this->t('The wave %label has been updated.', $message_args));
$this->logger('knowledge')->notice('The wave %label has been updated.', $logger_args);
break;
default:
throw new \LogicException('Could not save the entity.');
}
$form_state->setRedirectUrl($this->entity->toUrl());
return $result;
}
}
