RecentlyReadTypeForm.php in Recently Read 8
File
src/Form/RecentlyReadTypeForm.php
View source
<?php
namespace Drupal\recently_read\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RecentlyReadTypeForm extends EntityForm {
protected $messenger;
protected $bundleInfo;
public function __construct(MessengerInterface $messenger, EntityTypeBundleInfoInterface $bundleInfo) {
$this->messenger = $messenger;
$this->bundleInfo = $bundleInfo;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'), $container
->get('entity_type.bundle.info'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$entity = $this->entity;
$bundles = $this->bundleInfo
->getBundleInfo($entity
->id());
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Entity type'),
'#default_value' => $entity
->label(),
'#disabled' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\recently_read\\Entity\\RecentlyReadType::load',
],
'#disabled' => TRUE,
];
$options = [];
foreach ($bundles as $key => $value) {
$options[$key] = ucfirst($key);
}
$form['types'] = [];
if (count($bundles) > 1) {
$form['types'] = [
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $entity
->get('types'),
'#title' => $this
->t('Track'),
'#required' => FALSE,
'#prefix' => '<div id="types">',
'#suffix' => '</div>',
];
}
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$recently_read_type = $this->entity;
$status = $recently_read_type
->save();
switch ($status) {
case SAVED_NEW:
$this->messenger
->addMessage($this
->t('Created the %label recently read config.', [
'%label' => $recently_read_type
->label(),
]));
break;
default:
$this->messenger
->addMessage($this
->t('Saved the recently read %label config.', [
'%label' => $recently_read_type
->label(),
]));
}
$form_state
->setRedirectUrl($recently_read_type
->toUrl('collection'));
}
}