Added Bank Account entity.

This commit is contained in:
naomi 2021-02-07 11:59:17 +00:00
parent 0399c83646
commit eee7604440
15 changed files with 682 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<?php
/**
* @file
* Contains oc_bank_account.page.inc.
*
* Page callback for Bank Account entities.
*/
use Drupal\Core\Render\Element;
/**
* Prepares variables for Bank Account templates.
*
* Default template: oc_bank_account.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_bank_account(array &$variables) {
// Fetch OCBankAccount Entity Object.
$oc_bank_account = $variables['elements']['#oc_bank_account'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}

View File

@ -51,3 +51,8 @@ entity.oc_event_type.add_form:
title: 'Add Event type' title: 'Add Event type'
appears_on: appears_on:
- entity.oc_event_type.collection - entity.oc_event_type.collection
entity.oc_bank_account.add_form:
route_name: entity.oc_bank_account.add_form
title: 'Add Bank Account'
appears_on:
- entity.oc_bank_account.collection

View File

@ -86,3 +86,17 @@ entity.oc_event_type.collection:
description: 'List Event type (bundles)' description: 'List Event type (bundles)'
parent: system.admin_structure parent: system.admin_structure
weight: 99 weight: 99
# Bank Account menu items definition
entity.oc_bank_account.collection:
title: 'Bank Account list'
route_name: entity.oc_bank_account.collection
description: 'List Bank Account entities'
parent: system.admin_structure
weight: 100
oc_bank_account.admin.structure.settings:
title: 'Bank Account settings'
description: 'Configure Bank Account entities'
route_name: oc_bank_account.settings
parent: system.admin_structure

View File

@ -106,3 +106,24 @@ entity.oc_event.delete_form:
base_route: entity.oc_event.canonical base_route: entity.oc_event.canonical
title: Delete title: Delete
weight: 10 weight: 10
# Bank Account routing definition
oc_bank_account.settings_tab:
route_name: oc_bank_account.settings
title: 'Settings'
base_route: oc_bank_account.settings
entity.oc_bank_account.canonical:
route_name: entity.oc_bank_account.canonical
base_route: entity.oc_bank_account.canonical
title: 'View'
entity.oc_bank_account.edit_form:
route_name: entity.oc_bank_account.edit_form
base_route: entity.oc_bank_account.canonical
title: 'Edit'
entity.oc_bank_account.delete_form:
route_name: entity.oc_bank_account.delete_form
base_route: entity.oc_bank_account.canonical
title: Delete
weight: 10

View File

@ -127,3 +127,23 @@ view unpublished event entities:
permission_callbacks: permission_callbacks:
- \Drupal\opencase_entities\OCEventPermissions::generatePermissions - \Drupal\opencase_entities\OCEventPermissions::generatePermissions
add bank account entities:
title: 'Create new Bank Account entities'
administer bank account entities:
title: 'Administer Bank Account entities'
description: 'Allow to access the administration form to configure Bank Account entities.'
restrict access: true
delete bank account entities:
title: 'Delete Bank Account entities'
edit bank account entities:
title: 'Edit Bank Account entities'
view published bank account entities:
title: 'View published Bank Account entities'
view unpublished bank account entities:
title: 'View unpublished Bank Account entities'

View File

@ -0,0 +1,204 @@
<?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;
use Drupal\user\UserInterface;
/**
* Defines the Bank Account entity.
*
* @ingroup opencase_entities
*
* @ContentEntityType(
* id = "oc_bank_account",
* label = @Translation("Bank Account"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_entities\OCBankAccountListBuilder",
* "views_data" = "Drupal\opencase_entities\Entity\OCBankAccountViewsData",
*
* "form" = {
* "default" = "Drupal\opencase_entities\Form\OCBankAccountForm",
* "add" = "Drupal\opencase_entities\Form\OCBankAccountForm",
* "edit" = "Drupal\opencase_entities\Form\OCBankAccountForm",
* "delete" = "Drupal\opencase_entities\Form\OCBankAccountDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\opencase_entities\OCBankAccountHtmlRouteProvider",
* },
* "access" = "Drupal\opencase_entities\OCBankAccountAccessControlHandler",
* },
* base_table = "oc_bank_account",
* translatable = FALSE,
* admin_permission = "administer bank account entities",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "published" = "status",
* },
* links = {
* "canonical" = "/admin/opencase/oc_bank_account/{oc_bank_account}",
* "add-form" = "/admin/opencase/oc_bank_account/add",
* "edit-form" = "/admin/opencase/oc_bank_account/{oc_bank_account}/edit",
* "delete-form" = "/admin/opencase/oc_bank_account/{oc_bank_account}/delete",
* "collection" = "/admin/opencase/oc_bank_account",
* },
* field_ui_base_route = "oc_bank_account.settings"
* )
*/
class OCBankAccount extends ContentEntityBase implements OCBankAccountInterface {
use EntityChangedTrait;
use EntityPublishedTrait;
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()->id(),
];
}
/**
* {@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 function getOwner() {
return $this->get('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('user_id', $account->id());
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['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Bank Account entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Bank Account 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 Bank Account 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.'));
return $fields;
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface for defining Bank Account entities.
*
* @ingroup opencase_entities
*/
interface OCBankAccountInterface extends ContentEntityInterface, EntityChangedInterface, EntityPublishedInterface, EntityOwnerInterface {
/**
* Add get/set methods for your configuration properties here.
*/
/**
* Gets the Bank Account name.
*
* @return string
* Name of the Bank Account.
*/
public function getName();
/**
* Sets the Bank Account name.
*
* @param string $name
* The Bank Account name.
*
* @return \Drupal\opencase_entities\Entity\OCBankAccountInterface
* The called Bank Account entity.
*/
public function setName($name);
/**
* Gets the Bank Account creation timestamp.
*
* @return int
* Creation timestamp of the Bank Account.
*/
public function getCreatedTime();
/**
* Sets the Bank Account creation timestamp.
*
* @param int $timestamp
* The Bank Account creation timestamp.
*
* @return \Drupal\opencase_entities\Entity\OCBankAccountInterface
* The called Bank Account 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 Bank Account entities.
*/
class OCBankAccountViewsData 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 Bank Account entities.
*
* @ingroup opencase_entities
*/
class OCBankAccountDeleteForm 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 Bank Account edit forms.
*
* @ingroup opencase_entities
*/
class OCBankAccountForm 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\OCBankAccount $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 Bank Account.', [
'%label' => $entity->label(),
]));
break;
default:
$this->messenger()->addMessage($this->t('Saved the %label Bank Account.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.oc_bank_account.canonical', ['oc_bank_account' => $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 OCBankAccountSettingsForm.
*
* @ingroup opencase_entities
*/
class OCBankAccountSettingsForm extends FormBase {
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'ocbankaccount_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 Bank Account 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['ocbankaccount_settings']['#markup'] = 'Settings form for Bank Account 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 Bank Account entity.
*
* @see \Drupal\opencase_entities\Entity\OCBankAccount.
*/
class OCBankAccountAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\opencase_entities\Entity\OCBankAccountInterface $entity */
switch ($operation) {
case 'view':
if (!$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished bank account entities');
}
return AccessResult::allowedIfHasPermission($account, 'view published bank account entities');
case 'update':
return AccessResult::allowedIfHasPermission($account, 'edit bank account entities');
case 'delete':
return AccessResult::allowedIfHasPermission($account, 'delete bank account entities');
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add bank account 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 Bank Account entities.
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class OCBankAccountHtmlRouteProvider 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\OCBankAccountSettingsForm',
'_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 Bank Account entities.
*
* @ingroup opencase_entities
*/
class OCBankAccountListBuilder extends EntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('Bank Account ID');
$header['name'] = $this->t('Name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var \Drupal\opencase_entities\Entity\OCBankAccount $entity */
$row['id'] = $entity->id();
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.oc_bank_account.edit_form',
['oc_bank_account' => $entity->id()]
);
return $row + parent::buildRow($entity);
}
}

View File

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