newly generated content entity.

This commit is contained in:
Naomi 2018-09-17 17:29:04 +01:00
parent 1e0a6a41d3
commit 7e1889346b
16 changed files with 708 additions and 0 deletions

View File

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

View File

@ -0,0 +1,5 @@
entity.opencase_report.add_form:
route_name: entity.opencase_report.add_form
title: 'Add OpenCase Report'
appears_on:
- entity.opencase_report.collection

View File

@ -0,0 +1,14 @@
# OpenCase Report menu items definition
entity.opencase_report.collection:
title: 'OpenCase Report list'
route_name: entity.opencase_report.collection
description: 'List OpenCase Report entities'
parent: system.admin_structure
weight: 100
opencase_report.admin.structure.settings:
title: 'OpenCase Report settings'
description: 'Configure OpenCase Report entities'
route_name: opencase_report.settings
parent: system.admin_structure

View File

@ -0,0 +1,22 @@
# OpenCase Report routing definition
opencase_report.settings_tab:
route_name: opencase_report.settings
title: 'Settings'
base_route: opencase_report.settings
entity.opencase_report.canonical:
route_name: entity.opencase_report.canonical
base_route: entity.opencase_report.canonical
title: 'View'
entity.opencase_report.edit_form:
route_name: entity.opencase_report.edit_form
base_route: entity.opencase_report.canonical
title: 'Edit'
entity.opencase_report.delete_form:
route_name: entity.opencase_report.delete_form
base_route: entity.opencase_report.canonical
title: Delete
weight: 10

View File

@ -0,0 +1,19 @@
add opencase report entities:
title: 'Create new OpenCase Report entities'
administer opencase report entities:
title: 'Administer OpenCase Report entities'
description: 'Allow to access the administration form to configure OpenCase Report entities.'
restrict access: true
delete opencase report entities:
title: 'Delete OpenCase Report entities'
edit opencase report entities:
title: 'Edit OpenCase Report entities'
view published opencase report entities:
title: 'View published OpenCase Report entities'
view unpublished opencase report entities:
title: 'View unpublished OpenCase Report entities'

View File

@ -0,0 +1,220 @@
<?php
namespace Drupal\opencase_reporting\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\EntityTypeInterface;
use Drupal\user\UserInterface;
/**
* Defines the OpenCase Report entity.
*
* @ingroup opencase_reporting
*
* @ContentEntityType(
* id = "opencase_report",
* label = @Translation("OpenCase Report"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_reporting\OpenCaseReportListBuilder",
* "views_data" = "Drupal\opencase_reporting\Entity\OpenCaseReportViewsData",
* "translation" = "Drupal\opencase_reporting\OpenCaseReportTranslationHandler",
*
* "form" = {
* "default" = "Drupal\opencase_reporting\Form\OpenCaseReportForm",
* "add" = "Drupal\opencase_reporting\Form\OpenCaseReportForm",
* "edit" = "Drupal\opencase_reporting\Form\OpenCaseReportForm",
* "delete" = "Drupal\opencase_reporting\Form\OpenCaseReportDeleteForm",
* },
* "access" = "Drupal\opencase_reporting\OpenCaseReportAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\opencase_reporting\OpenCaseReportHtmlRouteProvider",
* },
* },
* base_table = "opencase_report",
* data_table = "opencase_report_field_data",
* translatable = TRUE,
* admin_permission = "administer opencase report entities",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* },
* links = {
* "canonical" = "/admin/structure/opencase_report/{opencase_report}",
* "add-form" = "/admin/structure/opencase_report/add",
* "edit-form" = "/admin/structure/opencase_report/{opencase_report}/edit",
* "delete-form" = "/admin/structure/opencase_report/{opencase_report}/delete",
* "collection" = "/admin/structure/opencase_report",
* },
* field_ui_base_route = "opencase_report.settings"
* )
*/
class OpenCaseReport extends ContentEntityBase implements OpenCaseReportInterface {
use EntityChangedTrait;
/**
* {@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 function isPublished() {
return (bool) $this->getEntityKey('status');
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? TRUE : FALSE);
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the OpenCase Report entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->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 OpenCase Report 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'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the OpenCase Report is published.'))
->setDefaultValue(TRUE)
->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,77 @@
<?php
namespace Drupal\opencase_reporting\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface for defining OpenCase Report entities.
*
* @ingroup opencase_reporting
*/
interface OpenCaseReportInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface {
// Add get/set methods for your configuration properties here.
/**
* Gets the OpenCase Report name.
*
* @return string
* Name of the OpenCase Report.
*/
public function getName();
/**
* Sets the OpenCase Report name.
*
* @param string $name
* The OpenCase Report name.
*
* @return \Drupal\opencase_reporting\Entity\OpenCaseReportInterface
* The called OpenCase Report entity.
*/
public function setName($name);
/**
* Gets the OpenCase Report creation timestamp.
*
* @return int
* Creation timestamp of the OpenCase Report.
*/
public function getCreatedTime();
/**
* Sets the OpenCase Report creation timestamp.
*
* @param int $timestamp
* The OpenCase Report creation timestamp.
*
* @return \Drupal\opencase_reporting\Entity\OpenCaseReportInterface
* The called OpenCase Report entity.
*/
public function setCreatedTime($timestamp);
/**
* Returns the OpenCase Report published status indicator.
*
* Unpublished OpenCase Report are only visible to restricted users.
*
* @return bool
* TRUE if the OpenCase Report is published.
*/
public function isPublished();
/**
* Sets the published status of a OpenCase Report.
*
* @param bool $published
* TRUE to set this OpenCase Report to published, FALSE to set it to unpublished.
*
* @return \Drupal\opencase_reporting\Entity\OpenCaseReportInterface
* The called OpenCase Report entity.
*/
public function setPublished($published);
}

View File

@ -0,0 +1,24 @@
<?php
namespace Drupal\opencase_reporting\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides Views data for OpenCase Report entities.
*/
class OpenCaseReportViewsData 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_reporting\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
/**
* Provides a form for deleting OpenCase Report entities.
*
* @ingroup opencase_reporting
*/
class OpenCaseReportDeleteForm extends ContentEntityDeleteForm {
}

View File

@ -0,0 +1,50 @@
<?php
namespace Drupal\opencase_reporting\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for OpenCase Report edit forms.
*
* @ingroup opencase_reporting
*/
class OpenCaseReportForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var $entity \Drupal\opencase_reporting\Entity\OpenCaseReport */
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
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:
drupal_set_message($this->t('Created the %label OpenCase Report.', [
'%label' => $entity->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label OpenCase Report.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.opencase_report.canonical', ['opencase_report' => $entity->id()]);
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace Drupal\opencase_reporting\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class OpenCaseReportSettingsForm.
*
* @ingroup opencase_reporting
*/
class OpenCaseReportSettingsForm extends FormBase {
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'opencasereport_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 OpenCase Report 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['opencasereport_settings']['#markup'] = 'Settings form for OpenCase Report entities. Manage field settings here.';
return $form;
}
}

View File

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

View File

@ -0,0 +1,56 @@
<?php
namespace Drupal\opencase_reporting;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for OpenCase Report entities.
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class OpenCaseReportHtmlRouteProvider 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_reporting\Form\OpenCaseReportSettingsForm',
'_title' => "{$entity_type->getLabel()} settings",
])
->setRequirement('_permission', $entity_type->getAdminPermission())
->setOption('_admin_route', TRUE);
return $route;
}
}
}

View File

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

View File

@ -0,0 +1,14 @@
<?php
namespace Drupal\opencase_reporting;
use Drupal\content_translation\ContentTranslationHandler;
/**
* Defines the translation handler for opencase_report.
*/
class OpenCaseReportTranslationHandler extends ContentTranslationHandler {
// Override here the needed methods from ContentTranslationHandler.
}

View File

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