diff --git a/recurring_events.routing.yml b/recurring_events.routing.yml index 53501a0f99b928d3c514c393888da4e42d49561e..9b110ab63e58ecd25ff37c854d58a78ac603c13b 100644 --- a/recurring_events.routing.yml +++ b/recurring_events.routing.yml @@ -88,7 +88,47 @@ entity.eventinstance.canonical: eventinstance: type: entity:eventinstance -# Admin Menu Strucutre. +# Edit an EventInstance. +entity.eventinstance.edit_form: + path: '/events/{eventinstance}/edit' + defaults: + _entity_form: eventinstance.edit + _title: 'Edit Event Instance' + requirements: + _entity_access: 'eventinstance.edit' + eventinstance: \d+ + options: + parameters: + eventinstance: + type: entity:eventinstance + +# Delete an EventInstance. +entity.eventinstance.delete_form: + path: '/events/{eventinstance}/delete' + defaults: + _entity_form: eventinstance.delete + _title: 'Delete Event Instance' + requirements: + _entity_access: 'eventinstance.delete' + eventinstance: \d+ + options: + parameters: + eventinstance: + type: entity:eventinstance + +# Event Instance table list route. +entity.eventinstance.collection: + path: '/events' + defaults: + _entity_list: 'eventinstance' + _title: 'Events' + requirements: + # Checks for permission directly. + _permission: 'access eventinstance overview' + +# Admin Pages. + +# Admin Menu Structure. events.admin.overview: path: '/admin/structure/events' defaults: diff --git a/src/Entity/EventInstance.php b/src/Entity/EventInstance.php index 21419699a8eaf6ad29151595eeae89a90856532d..d73c34177ea41fd3a28b9803d2dd465a970ef564 100644 --- a/src/Entity/EventInstance.php +++ b/src/Entity/EventInstance.php @@ -330,7 +330,7 @@ class EventInstance extends EditorialContentEntityBase implements EventInterface ->setDisplayConfigurable('form', TRUE); $fields['date'] = BaseFieldDefinition::create('daterange') - ->setLabel(t('Event Description')) + ->setLabel(t('Event Date')) ->setTranslatable(FALSE) ->setRevisionable(TRUE) ->setRequired(TRUE) @@ -377,4 +377,14 @@ class EventInstance extends EditorialContentEntityBase implements EventInterface return $fields; } + /** + * Get event series. + * + * @return Drupal\recurring_events\EventInterface + * The event series. + */ + public function getEventSeries() { + return $this->get('eventseries_id')->entity; + } + } diff --git a/src/Form/EventInstanceDeleteForm.php b/src/Form/EventInstanceDeleteForm.php new file mode 100644 index 0000000000000000000000000000000000000000..57c5bcb3176713706500f60d169c1591cd0f7322 --- /dev/null +++ b/src/Form/EventInstanceDeleteForm.php @@ -0,0 +1,182 @@ +<?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; + +/** + * Provides a form for deleting an eventinstance entity. + * + * @ingroup recurring_event + */ +class EventInstanceDeleteForm extends ContentEntityDeleteForm { + + /** + * The untranslated event instance. + * + * @var \Drupal\Core\Entity\ContentEntityInterface + */ + public $untranslatedEventInstance; + + /** + * The messenger service. + * + * @var Drupal\Core\Messenger\Messenger + */ + protected $messenger; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager'), + $container->get('messenger') + ); + } + + /** + * Construct a EventSeriesEditForm. + * + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager service. + * @param \Drupal\Core\Messenger\Messenger $messenger + * The messenger service. + */ + public function __construct(EntityManagerInterface $entity_manager, Messenger $messenger) { + $this->messenger = $messenger; + 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->entity->getEventSeries()->title->value, + ]); + } + return $this->t('Are you sure you want to delete event instance for %name?', ['%name' => $this->entity->getEventSeries()->title->value]); + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $this->getEntity(); + + // Make sure that deleting a translation does not delete the whole entity. + if ($entity->isDefaultTranslation()) { + $start_date = $entity->date->start_date; + return $this->t('Deleting this instance will remove only the instance on %date and not other events in this series. This action cannot be undone.', [ + '%date' => \Drupal::service('date.formatter')->format($start_date->getTimestamp(), 'custom', 'Y-m-d h:i A'), + ]); + } + } + + /** + * {@inheritdoc} + * + * If the delete command is canceled, return to the eventinstance list. + */ + public function getCancelUrl() { + return new Url('entity.eventinstance.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->untranslatedEventInstance = $entity->getUntranslated(); + if (!$entity->isDefaultTranslation()) { + $this->untranslatedEventInstance->removeTranslation($entity->language()->getId()); + $this->untranslatedEventInstance->save(); + $form_state->setRedirectUrl($this->untranslatedEventInstance->toUrl('canonical')); + } + else { + $series_instances = $entity->getEventSeries()->event_instances->referencedEntities(); + + // Loop through all instances on the series and remove the reference to + // this instance. + if (!empty($series_instances)) { + $changed = FALSE; + foreach ($series_instances as $index => $instance) { + if ($instance->id() == $entity->id()) { + $entity->getEventSeries()->event_instances->removeItem($index); + $changed = TRUE; + } + } + } + + // If changes were made to the series entity, save it. + if ($changed) { + $entity->getEventSeries()->save(); + } + + $entity->delete(); + + $start_date = $entity->date->start_date; + \Drupal::logger('omega_events')->notice('@type: deleted event instance of %title scheduled to begin on %date.', + [ + '@type' => $this->entity->bundle(), + '%title' => $this->entity->getEventSeries()->title->value, + '%date' => \Drupal::service('date.formatter')->format($start_date->getTimestamp(), 'custom', 'Y-m-d h:i A'), + ] + ); + + $this->messenger->addMessage($this->t('The %title event instance starting on %date has been deleted.', [ + '%title' => $this->entity->getEventSeries()->title->value, + '%date' => \Drupal::service('date.formatter')->format($start_date->getTimestamp(), 'custom', 'Y-m-d h:i A'), + ])); + + $form_state->setRedirect('entity.eventinstance.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->untranslatedEventInstance->getEventSeries()->title->value, + ]); + } + + return $this->t('The @type %title has been deleted.', [ + '@type' => 'Event Instance', + '%title' => $this->untranslatedEventInstance->getEventSeries()->title->value, + ]); + } + +} diff --git a/src/Form/EventInstanceForm.php b/src/Form/EventInstanceForm.php new file mode 100644 index 0000000000000000000000000000000000000000..12caf3dfcc7abf630656e41133c045ce5267650b --- /dev/null +++ b/src/Form/EventInstanceForm.php @@ -0,0 +1,71 @@ +<?php + +namespace Drupal\recurring_events\Form; + +use Drupal\Core\Entity\ContentEntityForm; +use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Messenger\Messenger; + +/** + * Form controller for the eventinstance entity edit forms. + * + * @ingroup recurring_events + */ +class EventInstanceForm extends ContentEntityForm { + + /** + * The messenger service. + * + * @var Drupal\Core\Messenger\Messenger + */ + protected $messenger; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager'), + $container->get('messenger') + ); + } + + /** + * Construct a EventSeriesEditForm. + * + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager service. + * @param \Drupal\Core\Messenger\Messenger $messenger + * The messenger service. + */ + public function __construct(EntityManagerInterface $entity_manager, Messenger $messenger) { + $this->messenger = $messenger; + parent::__construct($entity_manager); + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $form_state->setRedirect('entity.eventinstance.collection'); + parent::save($form, $form_state); + + $entity = $this->getEntity(); + + if ($entity->isDefaultTranslation()) { + $message = t('Event instance of %label has been saved.', [ + '%label' => $entity->getEventSeries()->title->value, + ]); + } + else { + $message = t('@language translation of the Event Instance %label has been saved.', [ + '@language' => $entity->language()->getName(), + '%label' => $entity->getUntranslated()->getEventSeries()->title->value, + ]); + } + $this->messenger->addMessage($message); + } + +}