Skip to content
Snippets Groups Projects
Commit 12cbb758 authored by Owen Bush's avatar Owen Bush
Browse files

Delete forms and instance list builder done

parent 32f0e61c
No related branches found
No related tags found
No related merge requests found
......@@ -73,6 +73,7 @@ use Drupal\user\UserInterface;
* label = @Translation("Event Instance entity"),
* handlers = {
* "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
* "list_builder" = "Drupal\recurring_events\EventInstanceListBuilder",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
......
......@@ -19,28 +19,28 @@ class EventCreationService {
/**
* The translation interface.
*
* @var Drupal\Core\StringTranslation\TranslationInterface
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
private $translation;
/**
* The database connection.
*
* @var Drupal\Core\Database\Connection
* @var \Drupal\Core\Database\Connection
*/
private $database;
/**
* Logger Factory.
*
* @var Drupal\Core\Logger\LoggerChannelFactoryInterface
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* The messenger service.
*
* @var Drupal\Core\Messenger\Messenger
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
......
<?php
namespace Drupal\recurring_events;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactory;
/**
* Provides a listing of eventinstance items.
*/
class EventInstanceListBuilder extends EntityListBuilder {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $config;
/**
* Constructs a new EventInstanceListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
* @param \Drupal\Core\Config\ConfigFactory $config
* The config factory service.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager, ConfigFactory $config) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
$this->languageManager = $language_manager;
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity.manager')->getStorage($entity_type->id()),
$container->get('date.formatter'),
$container->get('language_manager'),
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = [];
$header += [
'name' => $this->t('Event Name'),
'date' => $this->t('Date'),
'author' => [
'data' => $this->t('Author'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'status' => $this->t('Status'),
'changed' => [
'data' => $this->t('Updated'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
];
// Enable language column if multiple languages are added.
if ($this->languageManager->isMultilingual()) {
$header['language'] = [
'data' => $this->t('Language'),
'class' => [RESPONSIVE_PRIORITY_LOW],
];
}
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\recurring_events\EventInterface $entity */
$row['name']['data'] = [
'#type' => 'link',
'#title' => $entity->getEventSeries()->label(),
'#url' => $entity->toUrl(),
];
$config = $this->config->get('recurring_events.eventseries.config');
$row['date'] = $entity->date->start_date->format($config->get('date_format'));
$row['author']['data'] = [
'#theme' => 'username',
'#account' => $entity->getOwner(),
];
$row['status'] = $entity->isPublished() ? $this->t('Published') : $this->t('Unpublished');
$row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
if ($this->languageManager->isMultilingual()) {
$row['language'] = $this->languageManager->getLanguageName($entity->language()->getId());
}
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
protected function getEntityIds() {
$query = $this->getStorage()->getQuery()
->sort('changed', 'DESC');
// Only add the pager if a limit is specified.
if ($this->limit) {
$query->pager($this->limit);
}
return $query->execute();
}
}
......@@ -38,7 +38,7 @@ class EventSeriesListBuilder extends EntityListBuilder {
protected $config;
/**
* Constructs a new MediaListBuilder object.
* Constructs a new EventSeriesListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
......
......@@ -26,7 +26,7 @@ class EventInstanceDeleteForm extends ContentEntityDeleteForm {
/**
* The messenger service.
*
* @var Drupal\Core\Messenger\Messenger
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
......@@ -41,7 +41,7 @@ class EventInstanceDeleteForm extends ContentEntityDeleteForm {
}
/**
* Construct a EventSeriesEditForm.
* Construct a EventInstanceDeleteForm.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
......
......@@ -18,7 +18,7 @@ class EventInstanceForm extends ContentEntityForm {
/**
* The messenger service.
*
* @var Drupal\Core\Messenger\Messenger
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
......@@ -33,7 +33,7 @@ class EventInstanceForm extends ContentEntityForm {
}
/**
* Construct a EventSeriesEditForm.
* Construct a EventSeriesForm.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
......
<?php
namespace Drupal\recurring_events\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Url;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Render\Renderer;
/**
* Provides a form for deleting an eventseries entity.
*
* @ingroup recurring_events
*/
class EventSeriesDeleteForm extends ContentEntityDeleteForm {
/**
* The untranslated eventseries.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
*/
public $untranslatedEvent;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\Renderer
*/
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('messenger'),
$container->get('renderer')
);
}
/**
* Construct a EventSeriesDeleteForm.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The messenger service.
* @param \Drupal\Core\Render\Renderer $renderer
* The renderer service.
*/
public function __construct(EntityManagerInterface $entity_manager, Messenger $messenger, Renderer $renderer) {
$this->messenger = $messenger;
$this->renderer = $renderer;
parent::__construct($entity_manager);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
if (!$entity->isDefaultTranslation()) {
return $this
->t('Are you sure you want to delete the @language translation of the @entity-type %label?', [
'@language' => $entity->language()->getName(),
'@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(),
'%label' => $this->getEntity()->title->value,
]);
}
return $this->t('Are you sure you want to delete event %name and its instances?', ['%name' => $this->entity->label()]);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
if ($this->entity->isDefaultTranslation()) {
$instances = $this->entity->event_instances->referencedEntities();
$description = [];
if (!empty($instances)) {
$description = [];
$description['description'] = [
'#type' => 'markup',
'#markup' => $this->t('This event series contains %count events taking place on the following dates:', ['%count' => count($instances)]),
];
$options = [];
foreach ($instances as $instance) {
$date = $instance->date->start_date;
$options[] = $instance->toLink($date->format('Y-m-d h:i A'));
}
$description['instances'] = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#title' => $this->t('Event Instances'),
'#items' => $options,
];
$description['end'] = [
'#type' => 'markup',
'#markup' => $this->t('Deleting this event series will remove all associated event instances.'),
];
}
$description['last'] = [
'#type' => 'markup',
'#markup' => $this->t('<p>This action cannot be undone.</p>'),
];
return $this->renderer->render($description);
}
return '';
}
/**
* {@inheritdoc}
*
* If the delete command is canceled, return to the eventinstance list.
*/
public function getCancelUrl() {
return new Url('entity.eventseries.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->getEntity();
// Make sure that deleting a translation does not delete the whole entity.
$this->untranslatedEvent = $entity->getUntranslated();
if (!$entity->isDefaultTranslation()) {
$this->untranslatedEvent->removeTranslation($entity->language()->getId());
$this->untranslatedEvent->save();
$form_state->setRedirectUrl($this->untranslatedEvent->toUrl('canonical'));
}
else {
$instances = $entity->event_instances->referencedEntities();
// Loop through all instances and remove them.
foreach ($instances as $instance) {
$instance->delete();
}
$entity->delete();
\Drupal::logger('recurring_events')->notice('@type: deleted %title.',
[
'@type' => $this->entity->bundle(),
'%title' => $this->entity->value,
]
);
$this->messenger->addMessage($this->t('The %title event series and all the instances have been deleted.', [
'%title' => $this->entity->title->value,
]));
$form_state->setRedirect('entity.eventseries.collection');
}
$this->messenger->addMessage($this->getDeletionMessage());
$this->logDeletionMessage();
}
/**
* {@inheritdoc}
*/
protected function getDeletionMessage() {
/** @var \Drupal\omega_events\EventInterface $entity */
$entity = $this->getEntity();
if (!$entity->isDefaultTranslation()) {
return $this->t('@language translation of the @type %label has been deleted.', [
'@language' => $entity->language()->getName(),
'@type' => 'Event',
'%label' => $this->untranslatedEvent->title->value,
]);
}
return $this->t('The @type %title has been deleted.', [
'@type' => 'Event',
'%title' => $this->untranslatedEvent->title->value,
]);
}
}
......@@ -28,28 +28,28 @@ class EventSeriesForm extends ContentEntityForm {
/**
* The event creation service.
*
* @var Drupal\recurring_events\EventCreationService
* @var \Drupal\recurring_events\EventCreationService
*/
protected $creationService;
/**
* The entity storage interface.
*
* @var Drupal\Core\Entity\EntityStorageInterface
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* The messenger service.
*
* @var Drupal\Core\Messenger\Messenger
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
/**
* The date formatter service.
*
* @var Drupal\Core\Datetime\DateFormatter
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateFormatter;
......@@ -67,7 +67,7 @@ class EventSeriesForm extends ContentEntityForm {
}
/**
* Construct a EventSeriesEditForm.
* Construct a EventSeriesForm.
*
* @param \Drupal\recurring_events\EventCreationService $creation_service
* The event creation service.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment