diff --git a/config/schema/oc_event.schema.yml b/config/schema/oc_event.schema.yml new file mode 100644 index 0000000..77de756 --- /dev/null +++ b/config/schema/oc_event.schema.yml @@ -0,0 +1,12 @@ +opencase.oc_event.*: + type: config_entity + label: 'Event config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/opencase_entities/config/schema/oc_event.schema.yml b/modules/opencase_entities/config/schema/oc_event.schema.yml new file mode 100644 index 0000000..fbdb323 --- /dev/null +++ b/modules/opencase_entities/config/schema/oc_event.schema.yml @@ -0,0 +1,12 @@ +opencase_entities.oc_event.*: + type: config_entity + label: 'Event config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/opencase_entities/config/schema/oc_event_type.schema.yml b/modules/opencase_entities/config/schema/oc_event_type.schema.yml new file mode 100644 index 0000000..5fb323f --- /dev/null +++ b/modules/opencase_entities/config/schema/oc_event_type.schema.yml @@ -0,0 +1,12 @@ +opencase_entities.oc_event_type.*: + type: config_entity + label: 'Event type config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/opencase_entities/oc_event.page.inc b/modules/opencase_entities/oc_event.page.inc new file mode 100644 index 0000000..10cf7a8 --- /dev/null +++ b/modules/opencase_entities/oc_event.page.inc @@ -0,0 +1,30 @@ + ['content' => NULL], 'file' => 'oc_organisation.page.inc', ]; + $theme['oc_event'] = [ + 'render element' => 'elements', + 'file' => 'oc_event.page.inc', + 'template' => 'oc_event', + ]; + $theme['oc_event_content_add_list'] = [ + 'render element' => 'content', + 'variables' => ['content' => NULL], + 'file' => 'oc_event.page.inc', + ]; return $theme; } @@ -165,3 +175,19 @@ function opencase_entities_theme_suggestions_oc_organisation(array $variables) { $suggestions[] = 'oc_organisation__' . $entity->id() . '__' . $sanitized_view_mode; return $suggestions; } + +/** + * Implements hook_theme_suggestions_HOOK(). + */ +function opencase_entities_theme_suggestions_oc_event(array $variables) { + $suggestions = []; + $entity = $variables['elements']['#oc_event']; + $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); + + $suggestions[] = 'oc_event__' . $sanitized_view_mode; + $suggestions[] = 'oc_event__' . $entity->bundle(); + $suggestions[] = 'oc_event__' . $entity->bundle() . '__' . $sanitized_view_mode; + $suggestions[] = 'oc_event__' . $entity->id(); + $suggestions[] = 'oc_event__' . $entity->id() . '__' . $sanitized_view_mode; + return $suggestions; +} diff --git a/modules/opencase_entities/opencase_entities.permissions.yml b/modules/opencase_entities/opencase_entities.permissions.yml index c77dc97..81a0a6d 100644 --- a/modules/opencase_entities/opencase_entities.permissions.yml +++ b/modules/opencase_entities/opencase_entities.permissions.yml @@ -105,3 +105,25 @@ delete all organisation revisions: permission_callbacks: - \Drupal\opencase_entities\OCOrganisationPermissions::generatePermissions +add event entities: + title: 'Create new Event entities' + +administer event entities: + title: 'Administer Event entities' + description: 'Allow to access the administration form to configure Event entities.' + restrict access: true + +delete event entities: + title: 'Delete Event entities' + +edit event entities: + title: 'Edit Event entities' + +view published event entities: + title: 'View published Event entities' + +view unpublished event entities: + title: 'View unpublished Event entities' + +permission_callbacks: + - \Drupal\opencase_entities\OCEventPermissions::generatePermissions diff --git a/modules/opencase_entities/src/Entity/OCEvent.php b/modules/opencase_entities/src/Entity/OCEvent.php new file mode 100644 index 0000000..edce002 --- /dev/null +++ b/modules/opencase_entities/src/Entity/OCEvent.php @@ -0,0 +1,159 @@ +get('name')->value; + } + + /** + * {@inheritdoc} + */ + public function setName($name) { + $this->set('name', $name); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getCreatedTime() { + return $this->get('created')->value; + } + + /** + * {@inheritdoc} + */ + public function setCreatedTime($timestamp) { + $this->set('created', $timestamp); + return $this; + } + + /** + * {@inheritdoc} + */ + public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { + $fields = parent::baseFieldDefinitions($entity_type); + + // Add the published field. + $fields += static::publishedBaseFieldDefinitions($entity_type); + + $fields['name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Name')) + ->setDescription(t('The name of the Event entity.')) + ->setSettings([ + 'max_length' => 50, + 'text_processing' => 0, + ]) + ->setDefaultValue('') + ->setDisplayOptions('view', [ + 'label' => 'above', + 'type' => 'string', + 'weight' => -4, + ]) + ->setDisplayOptions('form', [ + 'type' => 'string_textfield', + 'weight' => -4, + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE) + ->setRequired(TRUE); + + $fields['status']->setDescription(t('A boolean indicating whether the Event is published.')); + + $fields['created'] = BaseFieldDefinition::create('created') + ->setLabel(t('Created')) + ->setDescription(t('The time that the entity was created.')); + + $fields['changed'] = BaseFieldDefinition::create('changed') + ->setLabel(t('Changed')) + ->setDescription(t('The time that the entity was last edited.')); + + $fields['activity_date_time'] = BaseFieldDefinition::create('datetime') + ->setLabel(t('Date and time')) + ->setRevisionable(TRUE) + ->setRequired(TRUE) + // Uses the currentDateTime function from the Activity entity + ->setDefaultValueCallback('\Drupal\opencase_entities\Entity\OCActivity::currentDateTime') + ->setDisplayOptions('view', [ + 'label' => 'above', + 'type' => 'datetime_default', + 'settings' => [ + 'format_type' => 'medium', + ], + 'weight' => -3, + ]) + ->setDisplayOptions('form', [ + 'type' => 'datetime_default', + 'weight' => -3, + ]); + + return $fields; + } + +} diff --git a/modules/opencase_entities/src/Entity/OCEventInterface.php b/modules/opencase_entities/src/Entity/OCEventInterface.php new file mode 100644 index 0000000..1651dbe --- /dev/null +++ b/modules/opencase_entities/src/Entity/OCEventInterface.php @@ -0,0 +1,58 @@ +account = $container->get('current_user'); + return $instance; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + /* @var \Drupal\opencase_entities\Entity\OCEvent $entity */ + $form = parent::buildForm($form, $form_state); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $entity = $this->entity; + + $status = parent::save($form, $form_state); + + switch ($status) { + case SAVED_NEW: + $this->messenger()->addMessage($this->t('Created the %label Event.', [ + '%label' => $entity->label(), + ])); + break; + + default: + $this->messenger()->addMessage($this->t('Saved the %label Event.', [ + '%label' => $entity->label(), + ])); + } + $form_state->setRedirect('entity.oc_event.canonical', ['oc_event' => $entity->id()]); + } + +} diff --git a/modules/opencase_entities/src/Form/OCEventSettingsForm.php b/modules/opencase_entities/src/Form/OCEventSettingsForm.php new file mode 100644 index 0000000..eaf22f8 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCEventSettingsForm.php @@ -0,0 +1,53 @@ +t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return new Url('entity.oc_event_type.collection'); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->entity->delete(); + + $this->messenger()->addMessage( + $this->t('content @type: deleted @label.', [ + '@type' => $this->entity->bundle(), + '@label' => $this->entity->label(), + ]) + ); + + $form_state->setRedirectUrl($this->getCancelUrl()); + } + +} diff --git a/modules/opencase_entities/src/Form/OCEventTypeForm.php b/modules/opencase_entities/src/Form/OCEventTypeForm.php new file mode 100644 index 0000000..8a018eb --- /dev/null +++ b/modules/opencase_entities/src/Form/OCEventTypeForm.php @@ -0,0 +1,65 @@ +entity; + $form['label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $oc_event_type->label(), + '#description' => $this->t("Label for the Event type."), + '#required' => TRUE, + ]; + + $form['id'] = [ + '#type' => 'machine_name', + '#default_value' => $oc_event_type->id(), + '#machine_name' => [ + 'exists' => '\Drupal\opencase_entities\Entity\OCEventType::load', + ], + '#disabled' => !$oc_event_type->isNew(), + ]; + + /* You will need additional form elements for your custom properties. */ + + return $form; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $oc_event_type = $this->entity; + $status = $oc_event_type->save(); + + switch ($status) { + case SAVED_NEW: + $this->messenger()->addMessage($this->t('Created the %label Event type.', [ + '%label' => $oc_event_type->label(), + ])); + break; + + default: + $this->messenger()->addMessage($this->t('Saved the %label Event type.', [ + '%label' => $oc_event_type->label(), + ])); + } + $form_state->setRedirectUrl($oc_event_type->toUrl('collection')); + } + +} diff --git a/modules/opencase_entities/src/OCEvent. b/modules/opencase_entities/src/OCEvent. new file mode 100644 index 0000000..e69de29 diff --git a/modules/opencase_entities/src/OCEventAccessControlHandler.php b/modules/opencase_entities/src/OCEventAccessControlHandler.php new file mode 100644 index 0000000..17fe59c --- /dev/null +++ b/modules/opencase_entities/src/OCEventAccessControlHandler.php @@ -0,0 +1,116 @@ +isPublished()) { + $permission = $this->checkOwn($entity, 'view unpublished', $account); + if (!empty($permission)) { + return AccessResult::allowed(); + } + + return AccessResult::allowedIfHasPermission($account, 'view unpublished event entities'); + } + + $permission = $this->checkOwn($entity, $operation, $account); + if (!empty($permission)) { + return AccessResult::allowed(); + } + + return AccessResult::allowedIfHasPermission($account, 'view published event entities'); + + case 'update': + + $permission = $this->checkOwn($entity, $operation, $account); + if (!empty($permission)) { + return AccessResult::allowed(); + } + return AccessResult::allowedIfHasPermission($account, 'edit event entities'); + + case 'delete': + + $permission = $this->checkOwn($entity, $operation, $account); + if (!empty($permission)) { + return AccessResult::allowed(); + } + return AccessResult::allowedIfHasPermission($account, 'delete event entities'); + } + + // Unknown operation, no opinion. + return AccessResult::neutral(); + } + + /** + * {@inheritdoc} + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + return AccessResult::allowedIfHasPermission($account, 'add event entities'); + } + + /** + * Test for given 'own' permission. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * @param $operation + * @param \Drupal\Core\Session\AccountInterface $account + * + * @return string|null + * The permission string indicating it's allowed. + */ + protected function checkOwn(EntityInterface $entity, $operation, AccountInterface $account) { + $status = $entity->isPublished(); + $uid = $entity->getOwnerId(); + + $is_own = $account->isAuthenticated() && $account->id() == $uid; + if (!$is_own) { + return; + } + + $bundle = $entity->bundle(); + + $ops = [ + 'create' => '%bundle add own %bundle entities', + 'view unpublished' => '%bundle view own unpublished %bundle entities', + 'view' => '%bundle view own entities', + 'update' => '%bundle edit own entities', + 'delete' => '%bundle delete own entities', + ]; + $permission = strtr($ops[$operation], ['%bundle' => $bundle]); + + if ($operation === 'view unpublished') { + if (!$status && $account->hasPermission($permission)) { + return $permission; + } + else { + return NULL; + } + } + if ($account->hasPermission($permission)) { + return $permission; + } + + return NULL; + } + +} diff --git a/modules/opencase_entities/src/OCEventHtmlRouteProvider.php b/modules/opencase_entities/src/OCEventHtmlRouteProvider.php new file mode 100644 index 0000000..5911d5c --- /dev/null +++ b/modules/opencase_entities/src/OCEventHtmlRouteProvider.php @@ -0,0 +1,56 @@ +id(); + + if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) { + $collection->add("$entity_type_id.settings", $settings_form_route); + } + + return $collection; + } + + /** + * Gets the settings form route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getSettingsFormRoute(EntityTypeInterface $entity_type) { + if (!$entity_type->getBundleEntityType()) { + $route = new Route("/admin/structure/{$entity_type->id()}/settings"); + $route + ->setDefaults([ + '_form' => 'Drupal\opencase_entities\Form\OCEventSettingsForm', + '_title' => "{$entity_type->getLabel()} settings", + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/modules/opencase_entities/src/OCEventListBuilder.php b/modules/opencase_entities/src/OCEventListBuilder.php new file mode 100644 index 0000000..7fe3cfc --- /dev/null +++ b/modules/opencase_entities/src/OCEventListBuilder.php @@ -0,0 +1,39 @@ +t('Event ID'); + $header['name'] = $this->t('Name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + /* @var \Drupal\opencase_entities\Entity\OCEvent $entity */ + $row['id'] = $entity->id(); + $row['name'] = Link::createFromRoute( + $entity->label(), + 'entity.oc_event.edit_form', + ['oc_event' => $entity->id()] + ); + return $row + parent::buildRow($entity); + } + +} diff --git a/modules/opencase_entities/src/OCEventPermissions.php b/modules/opencase_entities/src/OCEventPermissions.php new file mode 100644 index 0000000..78bc4b1 --- /dev/null +++ b/modules/opencase_entities/src/OCEventPermissions.php @@ -0,0 +1,68 @@ +buildPermissions($type); + } + + return $perms; + } + + /** + * Returns a list of node permissions for a given node type. + * + * @param \Drupal\opencase_entities\Entity\OCEvent $type + * The OCEvent type. + * + * @return array + * An associative array of permission names and descriptions. + */ + protected function buildPermissions(OCEvent $type) { + $type_id = $type->id(); + $type_params = ['%type_name' => $type->label()]; + + return [ + "$type_id create entities" => [ + 'title' => $this->t('Create new %type_name entities', $type_params), + ], + "$type_id edit own entities" => [ + 'title' => $this->t('Edit own %type_name entities', $type_params), + ], + "$type_id edit any entities" => [ + 'title' => $this->t('Edit any %type_name entities', $type_params), + ], + "$type_id delete own entities" => [ + 'title' => $this->t('Delete own %type_name entities', $type_params), + ], + "$type_id delete any entities" => [ + 'title' => $this->t('Delete any %type_name entities', $type_params), + ], + ]; + } + +} diff --git a/modules/opencase_entities/src/OCEventTypeHtmlRouteProvider.php b/modules/opencase_entities/src/OCEventTypeHtmlRouteProvider.php new file mode 100644 index 0000000..c9d2387 --- /dev/null +++ b/modules/opencase_entities/src/OCEventTypeHtmlRouteProvider.php @@ -0,0 +1,26 @@ +t('Event type'); + $header['id'] = $this->t('Machine name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + $row['label'] = $entity->label(); + $row['id'] = $entity->id(); + // You probably want a few more properties here... + return $row + parent::buildRow($entity); + } + +} diff --git a/modules/opencase_entities/templates/oc-event-content-add-list.html.twig b/modules/opencase_entities/templates/oc-event-content-add-list.html.twig new file mode 100644 index 0000000..fec2daf --- /dev/null +++ b/modules/opencase_entities/templates/oc-event-content-add-list.html.twig @@ -0,0 +1,23 @@ +{# +/** + * @file + * Default theme implementation to present a list of custom content entity types/bundles. + * + * Available variables: + * - types: A collection of all the available custom entity types/bundles. + * Each type/bundle contains the following: + * - link: A link to add a content entity of this type. + * - description: A description of this content entity types/bundle. + * + * @see template_preprocess_oc_event_content_add_list() + * + * @ingroup themeable + */ +#} +{% spaceless %} +
+ {% for type in types %} +
{{ type.link }}
+ {% endfor %} +
+{% endspaceless %} diff --git a/modules/opencase_entities/templates/oc_event.html.twig b/modules/opencase_entities/templates/oc_event.html.twig new file mode 100644 index 0000000..4c90832 --- /dev/null +++ b/modules/opencase_entities/templates/oc_event.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file oc_event.html.twig + * Default theme implementation to present Event data. + * + * This template is used when viewing Event pages. + * + * + * Available variables: + * - content: A list of content items. Use 'content' to print all content, or + * - attributes: HTML attributes for the container element. + * + * @see template_preprocess_oc_event() + * + * @ingroup themeable + */ +#} + + {% if content %} + {{- content -}} + {% endif %} + diff --git a/opencase.links.action.yml b/opencase.links.action.yml new file mode 100644 index 0000000..df01e64 --- /dev/null +++ b/opencase.links.action.yml @@ -0,0 +1,5 @@ +entity.oc_event.add_form: + route_name: entity.oc_event.add_form + title: 'Add Event' + appears_on: + - entity.oc_event.collection diff --git a/opencase.links.menu.yml b/opencase.links.menu.yml index ff054ef..cc59463 100644 --- a/opencase.links.menu.yml +++ b/opencase.links.menu.yml @@ -18,6 +18,11 @@ opencase.add_organisations_links: deriver: Drupal\opencase\Plugin\Derivative\AddOrganisationsMenuLink menu_name: opencase parent: opencase.opencase_add_new_things_menu +opencase.add_events_links: + class: Drupal\opencase\Plugin\Menu\AddEventsMenuLink + deriver: Drupal\opencase\Plugin\Derivative\AddEventsMenuLink + menu_name: opencase + parent: opencase.opencase_add_new_things_menu opencase.opencase_admin_menu: title: 'Administration' description: 'Management and Configuration' @@ -59,6 +64,13 @@ opencase.manage_activity_types: parent: opencase.opencase_admin_menu url: internal:/admin/opencase/oc_activity_type weight: 3 +opencase.manage_event_types: + title: 'Manage event types' + description: 'Configure types of people and their fields' + menu_name: opencase + parent: opencase.opencase_admin_menu + url: internal:/admin/opencase/oc_event_type + weight: 1 opencase.opencase_support_menu: title: 'Support' diff --git a/src/Form/OCEventDeleteForm.php b/src/Form/OCEventDeleteForm.php new file mode 100644 index 0000000..78a57cc --- /dev/null +++ b/src/Form/OCEventDeleteForm.php @@ -0,0 +1,51 @@ +t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return new Url('entity.oc_event.collection'); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->entity->delete(); + + $this->messenger()->addMessage( + $this->t('content @type: deleted @label.', [ + '@type' => $this->entity->bundle(), + '@label' => $this->entity->label(), + ]) + ); + + $form_state->setRedirectUrl($this->getCancelUrl()); + } + +} diff --git a/src/Form/OCEventForm.php b/src/Form/OCEventForm.php new file mode 100644 index 0000000..470aadd --- /dev/null +++ b/src/Form/OCEventForm.php @@ -0,0 +1,65 @@ +entity; + $form['label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $oc_event->label(), + '#description' => $this->t("Label for the Event."), + '#required' => TRUE, + ]; + + $form['id'] = [ + '#type' => 'machine_name', + '#default_value' => $oc_event->id(), + '#machine_name' => [ + 'exists' => '\Drupal\opencase\Entity\OCEvent::load', + ], + '#disabled' => !$oc_event->isNew(), + ]; + + /* You will need additional form elements for your custom properties. */ + + return $form; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $oc_event = $this->entity; + $status = $oc_event->save(); + + switch ($status) { + case SAVED_NEW: + $this->messenger()->addMessage($this->t('Created the %label Event.', [ + '%label' => $oc_event->label(), + ])); + break; + + default: + $this->messenger()->addMessage($this->t('Saved the %label Event.', [ + '%label' => $oc_event->label(), + ])); + } + $form_state->setRedirectUrl($oc_event->toUrl('collection')); + } + +} diff --git a/src/OCEventHtmlRouteProvider.php b/src/OCEventHtmlRouteProvider.php new file mode 100644 index 0000000..ed16dfd --- /dev/null +++ b/src/OCEventHtmlRouteProvider.php @@ -0,0 +1,26 @@ +t('Event'); + $header['id'] = $this->t('Machine name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + $row['label'] = $entity->label(); + $row['id'] = $entity->id(); + // You probably want a few more properties here... + return $row + parent::buildRow($entity); + } + +} diff --git a/src/Plugin/Derivative/AddEventsMenuLink.php b/src/Plugin/Derivative/AddEventsMenuLink.php new file mode 100644 index 0000000..1cf9345 --- /dev/null +++ b/src/Plugin/Derivative/AddEventsMenuLink.php @@ -0,0 +1,55 @@ +entityTypeManager = $entity_type_manager; + } + + /** +   * {@inheritdoc} +   */ + public static function create(ContainerInterface $container, $base_plugin_id) { + return new static( + $base_plugin_id, + $container->get('entity_type.manager') + ); + } + /** +   * {@inheritdoc} +   */ + public function getDerivativeDefinitions($base_plugin_definition) { + $links = []; + $eventTypes = $this->entityTypeManager->getStorage('oc_event_type')->loadMultiple(); + foreach ($eventTypes as $id => $eventType) { + $links[$id] = [ + 'title' => "Add ". $eventType->label() . " Event", + 'route_name' => "entity.oc_event.add_form", + 'route_parameters' => ['oc_event_type' => $eventType->id()] + ] + $base_plugin_definition; + } + return $links; + } +} diff --git a/src/Plugin/Menu/AddEventsMenuLink.php b/src/Plugin/Menu/AddEventsMenuLink.php new file mode 100644 index 0000000..243288c --- /dev/null +++ b/src/Plugin/Menu/AddEventsMenuLink.php @@ -0,0 +1,6 @@ +