Added equal opps entity

This commit is contained in:
naomi 2021-02-18 15:32:10 +00:00
parent 34a2d9e23b
commit fbdc64de3c
16 changed files with 629 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<?php
/**
* @file
* Contains oc_equal_opps.page.inc.
*
* Page callback for Equal Opps entities.
*/
use Drupal\Core\Render\Element;
/**
* Prepares variables for Equal Opps templates.
*
* Default template: oc_equal_opps.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_oc_equal_opps(array &$variables) {
// Fetch OCEqualOpps Entity Object.
$oc_equal_opps = $variables['elements']['#oc_equal_opps'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}

View File

@ -45,3 +45,8 @@ entity.oc_bank_account.add_form:
title: 'Add Bank Account' title: 'Add Bank Account'
appears_on: appears_on:
- entity.oc_bank_account.collection - entity.oc_bank_account.collection
entity.oc_equal_opps.add_form:
route_name: entity.oc_equal_opps.add_form
title: 'Add Equal Opps'
appears_on:
- entity.oc_equal_opps.collection

View File

@ -82,3 +82,17 @@ oc_bank_account.admin.structure.settings:
description: 'Configure Bank Account entities' description: 'Configure Bank Account entities'
route_name: oc_bank_account.settings route_name: oc_bank_account.settings
parent: system.admin_structure parent: system.admin_structure
# Equal Opps menu items definition
entity.oc_equal_opps.collection:
title: 'Equal Opps list'
route_name: entity.oc_equal_opps.collection
description: 'List Equal Opps entities'
parent: system.admin_structure
weight: 100
oc_equal_opps.admin.structure.settings:
title: 'Equal Opps settings'
description: 'Configure Equal Opps entities'
route_name: oc_equal_opps.settings
parent: system.admin_structure

View File

@ -105,3 +105,24 @@ entity.oc_bank_account.delete_form:
base_route: entity.oc_bank_account.canonical base_route: entity.oc_bank_account.canonical
title: Delete title: Delete
weight: 10 weight: 10
# Equal Opps routing definition
oc_equal_opps.settings_tab:
route_name: oc_equal_opps.settings
title: 'Settings'
base_route: oc_equal_opps.settings
entity.oc_equal_opps.canonical:
route_name: entity.oc_equal_opps.canonical
base_route: entity.oc_equal_opps.canonical
title: 'View'
entity.oc_equal_opps.edit_form:
route_name: entity.oc_equal_opps.edit_form
base_route: entity.oc_equal_opps.canonical
title: 'Edit'
entity.oc_equal_opps.delete_form:
route_name: entity.oc_equal_opps.delete_form
base_route: entity.oc_equal_opps.canonical
title: Delete
weight: 10

View File

@ -157,3 +157,23 @@ view published bank account entities:
view unpublished bank account entities: view unpublished bank account entities:
title: 'View unpublished Bank Account entities' title: 'View unpublished Bank Account entities'
add equal opps entities:
title: 'Create new Equal Opps entities'
administer equal opps entities:
title: 'Administer Equal Opps entities'
description: 'Allow to access the administration form to configure Equal Opps entities.'
restrict access: true
delete equal opps entities:
title: 'Delete Equal Opps entities'
edit equal opps entities:
title: 'Edit Equal Opps entities'
view published equal opps entities:
title: 'View published Equal Opps entities'
view unpublished equal opps entities:
title: 'View unpublished Equal Opps entities'

View File

@ -0,0 +1,146 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Defines the Equal Opps entity.
*
* @ingroup opencase_entities
*
* @ContentEntityType(
* id = "oc_equal_opps",
* label = @Translation("Equal Opps"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_entities\OCEqualOppsListBuilder",
* "views_data" = "Drupal\opencase_entities\Entity\OCEqualOppsViewsData",
*
* "form" = {
* "default" = "Drupal\opencase_entities\Form\OCEqualOppsForm",
* "add" = "Drupal\opencase_entities\Form\OCEqualOppsForm",
* "edit" = "Drupal\opencase_entities\Form\OCEqualOppsForm",
* "delete" = "Drupal\opencase_entities\Form\OCEqualOppsDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\opencase_entities\OCEqualOppsHtmlRouteProvider",
* },
* "access" = "Drupal\opencase_entities\OCEqualOppsAccessControlHandler",
* },
* base_table = "oc_equal_opps",
* translatable = FALSE,
* admin_permission = "administer equal opps entities",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* "langcode" = "langcode",
* "published" = "status",
* },
* links = {
* "canonical" = "/opencase/oc_equal_opps/{oc_equal_opps}",
* "add-form" = "/opencase/oc_equal_opps/add",
* "edit-form" = "/opencase/oc_equal_opps/{oc_equal_opps}/edit",
* "delete-form" = "/opencase/oc_equal_opps/{oc_equal_opps}/delete",
* "collection" = "/opencase/oc_equal_opps",
* },
* field_ui_base_route = "oc_equal_opps.settings"
* )
*/
class OCEqualOpps extends ContentEntityBase implements OCEqualOppsInterface {
use EntityChangedTrait;
use EntityPublishedTrait;
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
// get the name of the person and make that the name of the thing.
$name = $this->get('oc_actor')->entity->get('name')->first()->value;
$this->setName($name);
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this->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 Equal Opps entity.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
]);
$fields['oc_actor'] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
->setLabel(t('Person'))
->setDescription(t('The person this pertains to.'))
->setSetting('target_type', 'oc_actor')
->setSetting('handler', 'default')
->setCardinality(1)
->setDisplayOptions('view', [
'type' => 'string',
'weight' => -3,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete_tags',
'weight' => -3,
])
->setRequired(TRUE);
$fields['status']->setDescription(t('A boolean indicating whether the Equal Opps 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.'));
return $fields;
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
/**
* Provides an interface for defining Equal Opps entities.
*
* @ingroup opencase_entities
*/
interface OCEqualOppsInterface extends ContentEntityInterface, EntityChangedInterface, EntityPublishedInterface {
/**
* Add get/set methods for your configuration properties here.
*/
/**
* Gets the Equal Opps name.
*
* @return string
* Name of the Equal Opps.
*/
public function getName();
/**
* Sets the Equal Opps name.
*
* @param string $name
* The Equal Opps name.
*
* @return \Drupal\opencase_entities\Entity\OCEqualOppsInterface
* The called Equal Opps entity.
*/
public function setName($name);
/**
* Gets the Equal Opps creation timestamp.
*
* @return int
* Creation timestamp of the Equal Opps.
*/
public function getCreatedTime();
/**
* Sets the Equal Opps creation timestamp.
*
* @param int $timestamp
* The Equal Opps creation timestamp.
*
* @return \Drupal\opencase_entities\Entity\OCEqualOppsInterface
* The called Equal Opps entity.
*/
public function setCreatedTime($timestamp);
}

View File

@ -0,0 +1,23 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides Views data for Equal Opps entities.
*/
class OCEqualOppsViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
// Additional information for Views integration, such as table joins, can be
// put here.
return $data;
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
/**
* Provides a form for deleting Equal Opps entities.
*
* @ingroup opencase_entities
*/
class OCEqualOppsDeleteForm extends ContentEntityDeleteForm {
}

View File

@ -0,0 +1,66 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for Equal Opps edit forms.
*
* @ingroup opencase_entities
*/
class OCEqualOppsForm extends ContentEntityForm {
/**
* The current user account.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $account;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
$instance = parent::create($container);
$instance->account = $container->get('current_user');
return $instance;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var \Drupal\opencase_entities\Entity\OCEqualOpps $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 Equal Opps.', [
'%label' => $entity->label(),
]));
break;
default:
$this->messenger()->addMessage($this->t('Saved the %label Equal Opps.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.oc_equal_opps.canonical', ['oc_equal_opps' => $entity->id()]);
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class OCEqualOppsSettingsForm.
*
* @ingroup opencase_entities
*/
class OCEqualOppsSettingsForm extends FormBase {
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'ocequalopps_settings';
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Empty implementation of the abstract submit class.
}
/**
* Defines the settings form for Equal Opps entities.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* Form definition array.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['ocequalopps_settings']['#markup'] = 'Settings form for Equal Opps entities. Manage field settings here.';
return $form;
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
/**
* Access controller for the Equal Opps entity.
*
* @see \Drupal\opencase_entities\Entity\OCEqualOpps.
*/
class OCEqualOppsAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\opencase_entities\Entity\OCEqualOppsInterface $entity */
switch ($operation) {
case 'view':
if (!$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished equal opps entities');
}
return AccessResult::allowedIfHasPermission($account, 'view published equal opps entities');
case 'update':
return AccessResult::allowedIfHasPermission($account, 'edit equal opps entities');
case 'delete':
return AccessResult::allowedIfHasPermission($account, 'delete equal opps entities');
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add equal opps entities');
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for Equal Opps entities.
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class OCEqualOppsHtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type->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\OCEqualOppsSettingsForm',
'_title' => "{$entity_type->getLabel()} settings",
])
->setRequirement('_permission', $entity_type->getAdminPermission())
->setOption('_admin_route', TRUE);
return $route;
}
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Link;
/**
* Defines a class to build a listing of Equal Opps entities.
*
* @ingroup opencase_entities
*/
class OCEqualOppsListBuilder extends EntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('Equal Opps ID');
$header['name'] = $this->t('Name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var \Drupal\opencase_entities\Entity\OCEqualOpps $entity */
$row['id'] = $entity->id();
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.oc_equal_opps.edit_form',
['oc_equal_opps' => $entity->id()]
);
return $row + parent::buildRow($entity);
}
}

View File

@ -0,0 +1,22 @@
{#
/**
* @file oc_equal_opps.html.twig
* Default theme implementation to present Equal Opps data.
*
* This template is used when viewing Equal Opps 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_equal_opps()
*
* @ingroup themeable
*/
#}
<div{{ attributes.addClass('oc_equal_opps') }}>
{% if content %}
{{- content -}}
{% endif %}
</div>

View File

@ -23,6 +23,12 @@ opencase.add_events_links:
deriver: Drupal\opencase\Plugin\Derivative\AddEventsMenuLink deriver: Drupal\opencase\Plugin\Derivative\AddEventsMenuLink
menu_name: opencase menu_name: opencase
parent: opencase.opencase_add_new_things_menu parent: opencase.opencase_add_new_things_menu
opencase.add_equal_opps:
title: 'Equal Opportunies record'
menu_name: opencase
url: internal:/opencase/oc_equal_opps/add
parent: opencase.opencase_add_new_things_menu
weight: 10
opencase.opencase_admin_menu: opencase.opencase_admin_menu:
title: 'Administration' title: 'Administration'
description: 'Management and Configuration' description: 'Management and Configuration'