From d151b1744e7f67133b175de9f69e09a4cbf50e18 Mon Sep 17 00:00:00 2001 From: Naomi Date: Fri, 16 Jul 2021 17:32:40 +0200 Subject: [PATCH] Added Organisation Relation entity with update hook to install it, bumped version --- .../oc_organisation_relation_type.schema.yml | 12 ++ .../oc_organisation_relation.page.inc | 30 ++++ .../opencase_entities.install | 6 + .../opencase_entities.links.action.yml | 10 ++ .../opencase_entities.links.menu.yml | 17 ++ .../opencase_entities.links.task.yml | 17 ++ .../opencase_entities.module | 26 +++ .../opencase_entities.permissions.yml | 20 +++ .../src/Entity/OCOrganisationRelation.php | 156 ++++++++++++++++++ .../OCOrganisationRelationInterface.php | 58 +++++++ .../src/Entity/OCOrganisationRelationType.php | 58 +++++++ .../OCOrganisationRelationTypeInterface.php | 13 ++ .../OCOrganisationRelationViewsData.php | 23 +++ .../Form/OCOrganisationRelationDeleteForm.php | 15 ++ .../src/Form/OCOrganisationRelationForm.php | 66 ++++++++ .../OCOrganisationRelationSettingsForm.php | 53 ++++++ .../OCOrganisationRelationTypeDeleteForm.php | 51 ++++++ .../Form/OCOrganisationRelationTypeForm.php | 65 ++++++++ ...ganisationRelationAccessControlHandler.php | 55 ++++++ ...COrganisationRelationHtmlRouteProvider.php | 56 +++++++ .../src/OCOrganisationRelationListBuilder.php | 39 +++++ ...anisationRelationTypeHtmlRouteProvider.php | 26 +++ .../OCOrganisationRelationTypeListBuilder.php | 32 ++++ ...sation-relation-content-add-list.html.twig | 23 +++ .../oc_organisation_relation.html.twig | 22 +++ opencase.info.yml | 2 +- 26 files changed, 950 insertions(+), 1 deletion(-) create mode 100644 modules/opencase_entities/config/schema/oc_organisation_relation_type.schema.yml create mode 100644 modules/opencase_entities/oc_organisation_relation.page.inc create mode 100644 modules/opencase_entities/src/Entity/OCOrganisationRelation.php create mode 100644 modules/opencase_entities/src/Entity/OCOrganisationRelationInterface.php create mode 100644 modules/opencase_entities/src/Entity/OCOrganisationRelationType.php create mode 100644 modules/opencase_entities/src/Entity/OCOrganisationRelationTypeInterface.php create mode 100644 modules/opencase_entities/src/Entity/OCOrganisationRelationViewsData.php create mode 100644 modules/opencase_entities/src/Form/OCOrganisationRelationDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCOrganisationRelationForm.php create mode 100644 modules/opencase_entities/src/Form/OCOrganisationRelationSettingsForm.php create mode 100644 modules/opencase_entities/src/Form/OCOrganisationRelationTypeDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCOrganisationRelationTypeForm.php create mode 100644 modules/opencase_entities/src/OCOrganisationRelationAccessControlHandler.php create mode 100644 modules/opencase_entities/src/OCOrganisationRelationHtmlRouteProvider.php create mode 100644 modules/opencase_entities/src/OCOrganisationRelationListBuilder.php create mode 100644 modules/opencase_entities/src/OCOrganisationRelationTypeHtmlRouteProvider.php create mode 100644 modules/opencase_entities/src/OCOrganisationRelationTypeListBuilder.php create mode 100644 modules/opencase_entities/templates/oc-organisation-relation-content-add-list.html.twig create mode 100644 modules/opencase_entities/templates/oc_organisation_relation.html.twig diff --git a/modules/opencase_entities/config/schema/oc_organisation_relation_type.schema.yml b/modules/opencase_entities/config/schema/oc_organisation_relation_type.schema.yml new file mode 100644 index 0000000..40e114d --- /dev/null +++ b/modules/opencase_entities/config/schema/oc_organisation_relation_type.schema.yml @@ -0,0 +1,12 @@ +opencase_entities.oc_organisation_relation_type.*: + type: config_entity + label: 'Organisation Relation type config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/opencase_entities/oc_organisation_relation.page.inc b/modules/opencase_entities/oc_organisation_relation.page.inc new file mode 100644 index 0000000..a08ef1d --- /dev/null +++ b/modules/opencase_entities/oc_organisation_relation.page.inc @@ -0,0 +1,30 @@ +clearCachedDefinitions(); + \Drupal::entityDefinitionUpdateManager() + ->installEntityType(\Drupal::entityTypeManager()->getDefinition('oc_organisation_relation')); +} + function opencase_entities_update_80201() { $update_manager = \Drupal::entityDefinitionUpdateManager(); $definition = $update_manager->getFieldStorageDefinition('phone', 'oc_actor'); diff --git a/modules/opencase_entities/opencase_entities.links.action.yml b/modules/opencase_entities/opencase_entities.links.action.yml index e505649..1c6b6c0 100644 --- a/modules/opencase_entities/opencase_entities.links.action.yml +++ b/modules/opencase_entities/opencase_entities.links.action.yml @@ -50,3 +50,13 @@ entity.oc_equal_opps.add_form: title: 'Add Equal Opps' appears_on: - entity.oc_equal_opps.collection +entity.oc_organisation_relation.add_form: + route_name: entity.oc_organisation_relation.add_page + title: 'Add Organisation Relation' + appears_on: + - entity.oc_organisation_relation.collection +entity.oc_organisation_relation_type.add_form: + route_name: entity.oc_organisation_relation_type.add_form + title: 'Add Organisation Relation type' + appears_on: + - entity.oc_organisation_relation_type.collection diff --git a/modules/opencase_entities/opencase_entities.links.menu.yml b/modules/opencase_entities/opencase_entities.links.menu.yml index 9b20c97..bb3cddf 100644 --- a/modules/opencase_entities/opencase_entities.links.menu.yml +++ b/modules/opencase_entities/opencase_entities.links.menu.yml @@ -96,3 +96,20 @@ oc_equal_opps.admin.structure.settings: description: 'Configure Equal Opps entities' route_name: oc_equal_opps.settings parent: system.admin_structure + +# Organisation Relation menu items definition +entity.oc_organisation_relation.collection: + title: 'Organisation Relation list' + route_name: entity.oc_organisation_relation.collection + description: 'List Organisation Relation entities' + parent: system.admin_structure + weight: 100 + + +# Organisation Relation type menu items definition +entity.oc_organisation_relation_type.collection: + title: 'Organisation Relation type' + route_name: entity.oc_organisation_relation_type.collection + description: 'List Organisation Relation type (bundles)' + parent: system.admin_structure + weight: 99 diff --git a/modules/opencase_entities/opencase_entities.links.task.yml b/modules/opencase_entities/opencase_entities.links.task.yml index c31ed13..19b29b5 100644 --- a/modules/opencase_entities/opencase_entities.links.task.yml +++ b/modules/opencase_entities/opencase_entities.links.task.yml @@ -126,3 +126,20 @@ entity.oc_equal_opps.delete_form: base_route: entity.oc_equal_opps.canonical title: Delete weight: 10 +# Organisation Relation routing definition + +entity.oc_organisation_relation.canonical: + route_name: entity.oc_organisation_relation.canonical + base_route: entity.oc_organisation_relation.canonical + title: 'View' + +entity.oc_organisation_relation.edit_form: + route_name: entity.oc_organisation_relation.edit_form + base_route: entity.oc_organisation_relation.canonical + title: 'Edit' + +entity.oc_organisation_relation.delete_form: + route_name: entity.oc_organisation_relation.delete_form + base_route: entity.oc_organisation_relation.canonical + title: Delete + weight: 10 diff --git a/modules/opencase_entities/opencase_entities.module b/modules/opencase_entities/opencase_entities.module index e3a6cd3..ab1d871 100644 --- a/modules/opencase_entities/opencase_entities.module +++ b/modules/opencase_entities/opencase_entities.module @@ -29,6 +29,16 @@ function opencase_entities_help($route_name, RouteMatchInterface $route_match) { */ function opencase_entities_theme() { $theme = []; + $theme['oc_organisation_relation'] = [ + 'render element' => 'elements', + 'file' => 'oc_organisation_relation.page.inc', + 'template' => 'oc_organisation_relation', + ]; + $theme['oc_organisation_relation_content_add_list'] = [ + 'render element' => 'content', + 'variables' => ['content' => NULL], + 'file' => 'oc_organisation_relation.page.inc', + ]; $theme['opencase_entities'] = [ 'render element' => 'children', ]; @@ -140,3 +150,19 @@ function opencase_entities_theme_suggestions_oc_event(array $variables) { return $suggestions; } + +/** + * Implements hook_theme_suggestions_HOOK(). + */ +function opencase_entities_theme_suggestions_oc_organisation_relation(array $variables) { + $suggestions = []; + $entity = $variables['elements']['#oc_organisation_relation']; + $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); + + $suggestions[] = 'oc_organisation_relation__' . $sanitized_view_mode; + $suggestions[] = 'oc_organisation_relation__' . $entity->bundle(); + $suggestions[] = 'oc_organisation_relation__' . $entity->bundle() . '__' . $sanitized_view_mode; + $suggestions[] = 'oc_organisation_relation__' . $entity->id(); + $suggestions[] = 'oc_organisation_relation__' . $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 6420f81..68d681a 100644 --- a/modules/opencase_entities/opencase_entities.permissions.yml +++ b/modules/opencase_entities/opencase_entities.permissions.yml @@ -177,3 +177,23 @@ view published equal opps entities: view unpublished equal opps entities: title: 'View unpublished Equal Opps entities' +add organisation relation entities: + title: 'Create new Organisation Relation entities' + +administer organisation relation entities: + title: 'Administer Organisation Relation entities' + description: 'Allow to access the administration form to configure Organisation Relation entities.' + restrict access: true + +delete organisation relation entities: + title: 'Delete Organisation Relation entities' + +edit organisation relation entities: + title: 'Edit Organisation Relation entities' + +view published organisation relation entities: + title: 'View published Organisation Relation entities' + +view unpublished organisation relation entities: + title: 'View unpublished Organisation Relation entities' + diff --git a/modules/opencase_entities/src/Entity/OCOrganisationRelation.php b/modules/opencase_entities/src/Entity/OCOrganisationRelation.php new file mode 100644 index 0000000..0b43948 --- /dev/null +++ b/modules/opencase_entities/src/Entity/OCOrganisationRelation.php @@ -0,0 +1,156 @@ +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 Organisation Relation entity.')) + ->setSettings([ + 'max_length' => 50, + 'text_processing' => 0, + ]) + ->setDefaultValue('Link between organisations') + ->setRequired(TRUE); + + $fields['status']->setDescription(t('A boolean indicating whether the Organisation Relation is published.')) + ->setDisplayOptions('form', [ + 'type' => 'boolean_checkbox', + 'weight' => -3, + ]); + + $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['organisations'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Organisations')) + ->setDescription(t('Linked organisations')) + ->setSetting('target_type', 'oc_organisation') + ->setSetting('handler', 'default') + ->setCardinality(2) + ->setDisplayOptions('form', [ + 'label' => 'above', + 'type' => 'entity_reference_autocomplete', + 'weight' => -2, + 'settings' => [ + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ], + ]) + ->setDisplayOptions('view', [ + 'label' => 'above', + ]) + ->setDefaultValueCallback('opencase_entities_organisation_relation_callback') + ->setRequired(TRUE); + return $fields; + } +} + +function opencase_entities_organisation_relation_callback() { + return [\Drupal::request()->query->get('organisation_id')]; +} + diff --git a/modules/opencase_entities/src/Entity/OCOrganisationRelationInterface.php b/modules/opencase_entities/src/Entity/OCOrganisationRelationInterface.php new file mode 100644 index 0000000..5283e4f --- /dev/null +++ b/modules/opencase_entities/src/Entity/OCOrganisationRelationInterface.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\OCOrganisationRelation $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 Organisation Relation.', [ + '%label' => $entity->label(), + ])); + break; + + default: + $this->messenger()->addMessage($this->t('Saved the %label Organisation Relation.', [ + '%label' => $entity->label(), + ])); + } + $form_state->setRedirect('entity.oc_organisation_relation.canonical', ['oc_organisation_relation' => $entity->id()]); + } + +} diff --git a/modules/opencase_entities/src/Form/OCOrganisationRelationSettingsForm.php b/modules/opencase_entities/src/Form/OCOrganisationRelationSettingsForm.php new file mode 100644 index 0000000..f51f513 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCOrganisationRelationSettingsForm.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_organisation_relation_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/OCOrganisationRelationTypeForm.php b/modules/opencase_entities/src/Form/OCOrganisationRelationTypeForm.php new file mode 100644 index 0000000..0a733d0 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCOrganisationRelationTypeForm.php @@ -0,0 +1,65 @@ +entity; + $form['label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $oc_organisation_relation_type->label(), + '#description' => $this->t("Label for the Organisation Relation type."), + '#required' => TRUE, + ]; + + $form['id'] = [ + '#type' => 'machine_name', + '#default_value' => $oc_organisation_relation_type->id(), + '#machine_name' => [ + 'exists' => '\Drupal\opencase_entities\Entity\OCOrganisationRelationType::load', + ], + '#disabled' => !$oc_organisation_relation_type->isNew(), + ]; + + /* You will need additional form elements for your custom properties. */ + + return $form; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $oc_organisation_relation_type = $this->entity; + $status = $oc_organisation_relation_type->save(); + + switch ($status) { + case SAVED_NEW: + $this->messenger()->addMessage($this->t('Created the %label Organisation Relation type.', [ + '%label' => $oc_organisation_relation_type->label(), + ])); + break; + + default: + $this->messenger()->addMessage($this->t('Saved the %label Organisation Relation type.', [ + '%label' => $oc_organisation_relation_type->label(), + ])); + } + $form_state->setRedirectUrl($oc_organisation_relation_type->toUrl('collection')); + } + +} diff --git a/modules/opencase_entities/src/OCOrganisationRelationAccessControlHandler.php b/modules/opencase_entities/src/OCOrganisationRelationAccessControlHandler.php new file mode 100644 index 0000000..effeba0 --- /dev/null +++ b/modules/opencase_entities/src/OCOrganisationRelationAccessControlHandler.php @@ -0,0 +1,55 @@ +isPublished()) { + return AccessResult::allowedIfHasPermission($account, 'view unpublished organisation relation entities'); + } + + + return AccessResult::allowedIfHasPermission($account, 'view published organisation relation entities'); + + case 'update': + + return AccessResult::allowedIfHasPermission($account, 'edit organisation relation entities'); + + case 'delete': + + return AccessResult::allowedIfHasPermission($account, 'delete organisation relation entities'); + } + + // Unknown operation, no opinion. + return AccessResult::neutral(); + } + + /** + * {@inheritdoc} + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + return AccessResult::allowedIfHasPermission($account, 'add organisation relation entities'); + } + + +} diff --git a/modules/opencase_entities/src/OCOrganisationRelationHtmlRouteProvider.php b/modules/opencase_entities/src/OCOrganisationRelationHtmlRouteProvider.php new file mode 100644 index 0000000..4a3b84b --- /dev/null +++ b/modules/opencase_entities/src/OCOrganisationRelationHtmlRouteProvider.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\OCOrganisationRelationSettingsForm', + '_title' => "{$entity_type->getLabel()} settings", + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/modules/opencase_entities/src/OCOrganisationRelationListBuilder.php b/modules/opencase_entities/src/OCOrganisationRelationListBuilder.php new file mode 100644 index 0000000..18afab8 --- /dev/null +++ b/modules/opencase_entities/src/OCOrganisationRelationListBuilder.php @@ -0,0 +1,39 @@ +t('Organisation Relation ID'); + $header['name'] = $this->t('Name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + /* @var \Drupal\opencase_entities\Entity\OCOrganisationRelation $entity */ + $row['id'] = $entity->id(); + $row['name'] = Link::createFromRoute( + $entity->label(), + 'entity.oc_organisation_relation.edit_form', + ['oc_organisation_relation' => $entity->id()] + ); + return $row + parent::buildRow($entity); + } + +} diff --git a/modules/opencase_entities/src/OCOrganisationRelationTypeHtmlRouteProvider.php b/modules/opencase_entities/src/OCOrganisationRelationTypeHtmlRouteProvider.php new file mode 100644 index 0000000..5591eaf --- /dev/null +++ b/modules/opencase_entities/src/OCOrganisationRelationTypeHtmlRouteProvider.php @@ -0,0 +1,26 @@ +t('Organisation Relation 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-organisation-relation-content-add-list.html.twig b/modules/opencase_entities/templates/oc-organisation-relation-content-add-list.html.twig new file mode 100644 index 0000000..f48a8c5 --- /dev/null +++ b/modules/opencase_entities/templates/oc-organisation-relation-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_organisation_relation_content_add_list() + * + * @ingroup themeable + */ +#} +{% spaceless %} +
+ {% for type in types %} +
{{ type.link }}
+ {% endfor %} +
+{% endspaceless %} diff --git a/modules/opencase_entities/templates/oc_organisation_relation.html.twig b/modules/opencase_entities/templates/oc_organisation_relation.html.twig new file mode 100644 index 0000000..9531eec --- /dev/null +++ b/modules/opencase_entities/templates/oc_organisation_relation.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file oc_organisation_relation.html.twig + * Default theme implementation to present Organisation Relation data. + * + * This template is used when viewing Organisation Relation 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_organisation_relation() + * + * @ingroup themeable + */ +#} + + {% if content %} + {{- content -}} + {% endif %} + diff --git a/opencase.info.yml b/opencase.info.yml index 5916ded..8bf6889 100644 --- a/opencase.info.yml +++ b/opencase.info.yml @@ -2,7 +2,7 @@ name: 'OpenCase' type: module description: 'Simple Case Management' core: 8.x -version: 8.x-2.0.1 +version: 8.x-2.0.2 package: 'OpenCase' dependencies: - opencase_entities