Generate case entity. (Called CaseEntity because "Case" is reserved word)

This commit is contained in:
naomi
2018-04-12 15:32:24 +02:00
parent 6746b986db
commit c9cf33ac19
25 changed files with 1028 additions and 0 deletions

View File

@ -0,0 +1,47 @@
<?php
namespace Drupal\zencrm_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 Case entity entity.
*
* @see \Drupal\zencrm_entities\Entity\CaseEntity.
*/
class CaseEntityAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\zencrm_entities\Entity\CaseEntityInterface $entity */
switch ($operation) {
case 'view':
if (!$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished case entity entities');
}
return AccessResult::allowedIfHasPermission($account, 'view published case entity entities');
case 'update':
return AccessResult::allowedIfHasPermission($account, 'edit case entity entities');
case 'delete':
return AccessResult::allowedIfHasPermission($account, 'delete case entity entities');
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add case entity entities');
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Drupal\zencrm_entities;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for Case entity entities.
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class CaseEntityHtmlRouteProvider 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\zencrm_entities\Form\CaseEntitySettingsForm',
'_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\zencrm_entities;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Link;
/**
* Defines a class to build a listing of Case entity entities.
*
* @ingroup zencrm_entities
*/
class CaseEntityListBuilder extends EntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('Case entity ID');
$header['name'] = $this->t('Name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var $entity \Drupal\zencrm_entities\Entity\CaseEntity */
$row['id'] = $entity->id();
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.case_entity.edit_form',
['case_entity' => $entity->id()]
);
return $row + parent::buildRow($entity);
}
}

View File

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

View File

@ -0,0 +1,28 @@
<?php
namespace Drupal\zencrm_entities;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for Case entity type entities.
*
* @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class CaseEntityTypeHtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
// Provide your custom entity routes here.
return $collection;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Drupal\zencrm_entities;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of Case entity type entities.
*/
class CaseEntityTypeListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Case entity 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);
}
}

View File

@ -0,0 +1,224 @@
<?php
namespace Drupal\zencrm_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\EntityTypeInterface;
use Drupal\user\UserInterface;
/**
* Defines the Case entity entity.
*
* @ingroup zencrm_entities
*
* @ContentEntityType(
* id = "case_entity",
* label = @Translation("Case entity"),
* bundle_label = @Translation("Case entity type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\zencrm_entities\CaseEntityListBuilder",
* "views_data" = "Drupal\zencrm_entities\Entity\CaseEntityViewsData",
* "translation" = "Drupal\zencrm_entities\CaseEntityTranslationHandler",
*
* "form" = {
* "default" = "Drupal\zencrm_entities\Form\CaseEntityForm",
* "add" = "Drupal\zencrm_entities\Form\CaseEntityForm",
* "edit" = "Drupal\zencrm_entities\Form\CaseEntityForm",
* "delete" = "Drupal\zencrm_entities\Form\CaseEntityDeleteForm",
* },
* "access" = "Drupal\zencrm_entities\CaseEntityAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\zencrm_entities\CaseEntityHtmlRouteProvider",
* },
* },
* base_table = "case_entity",
* data_table = "case_entity_field_data",
* translatable = TRUE,
* admin_permission = "administer case entity entities",
* entity_keys = {
* "id" = "id",
* "bundle" = "type",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* },
* links = {
* "canonical" = "/admin/structure/case_entity/{case_entity}",
* "add-page" = "/admin/structure/case_entity/add",
* "add-form" = "/admin/structure/case_entity/add/{case_entity_type}",
* "edit-form" = "/admin/structure/case_entity/{case_entity}/edit",
* "delete-form" = "/admin/structure/case_entity/{case_entity}/delete",
* "collection" = "/admin/structure/case_entity",
* },
* bundle_entity_type = "case_entity_type",
* field_ui_base_route = "entity.case_entity_type.edit_form"
* )
*/
class CaseEntity extends ContentEntityBase implements CaseEntityInterface {
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 Case entity 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 Case entity 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 Case entity 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\zencrm_entities\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface for defining Case entity entities.
*
* @ingroup zencrm_entities
*/
interface CaseEntityInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface {
// Add get/set methods for your configuration properties here.
/**
* Gets the Case entity name.
*
* @return string
* Name of the Case entity.
*/
public function getName();
/**
* Sets the Case entity name.
*
* @param string $name
* The Case entity name.
*
* @return \Drupal\zencrm_entities\Entity\CaseEntityInterface
* The called Case entity entity.
*/
public function setName($name);
/**
* Gets the Case entity creation timestamp.
*
* @return int
* Creation timestamp of the Case entity.
*/
public function getCreatedTime();
/**
* Sets the Case entity creation timestamp.
*
* @param int $timestamp
* The Case entity creation timestamp.
*
* @return \Drupal\zencrm_entities\Entity\CaseEntityInterface
* The called Case entity entity.
*/
public function setCreatedTime($timestamp);
/**
* Returns the Case entity published status indicator.
*
* Unpublished Case entity are only visible to restricted users.
*
* @return bool
* TRUE if the Case entity is published.
*/
public function isPublished();
/**
* Sets the published status of a Case entity.
*
* @param bool $published
* TRUE to set this Case entity to published, FALSE to set it to unpublished.
*
* @return \Drupal\zencrm_entities\Entity\CaseEntityInterface
* The called Case entity entity.
*/
public function setPublished($published);
}

View File

@ -0,0 +1,58 @@
<?php
namespace Drupal\zencrm_entities\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
/**
* Defines the Case entity type entity.
*
* @ConfigEntityType(
* id = "case_entity_type",
* label = @Translation("Case entity type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\zencrm_entities\CaseEntityTypeListBuilder",
* "form" = {
* "add" = "Drupal\zencrm_entities\Form\CaseEntityTypeForm",
* "edit" = "Drupal\zencrm_entities\Form\CaseEntityTypeForm",
* "delete" = "Drupal\zencrm_entities\Form\CaseEntityTypeDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\zencrm_entities\CaseEntityTypeHtmlRouteProvider",
* },
* },
* config_prefix = "case_entity_type",
* admin_permission = "administer site configuration",
* bundle_of = "case_entity",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "canonical" = "/admin/structure/case_entity_type/{case_entity_type}",
* "add-form" = "/admin/structure/case_entity_type/add",
* "edit-form" = "/admin/structure/case_entity_type/{case_entity_type}/edit",
* "delete-form" = "/admin/structure/case_entity_type/{case_entity_type}/delete",
* "collection" = "/admin/structure/case_entity_type"
* }
* )
*/
class CaseEntityType extends ConfigEntityBundleBase implements CaseEntityTypeInterface {
/**
* The Case entity type ID.
*
* @var string
*/
protected $id;
/**
* The Case entity type label.
*
* @var string
*/
protected $label;
}

View File

@ -0,0 +1,13 @@
<?php
namespace Drupal\zencrm_entities\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface for defining Case entity type entities.
*/
interface CaseEntityTypeInterface extends ConfigEntityInterface {
// Add get/set methods for your configuration properties here.
}

View File

@ -0,0 +1,24 @@
<?php
namespace Drupal\zencrm_entities\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides Views data for Case entity entities.
*/
class CaseEntityViewsData 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\zencrm_entities\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
/**
* Provides a form for deleting Case entity entities.
*
* @ingroup zencrm_entities
*/
class CaseEntityDeleteForm extends ContentEntityDeleteForm {
}

View File

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

View File

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

View File

@ -0,0 +1,53 @@
<?php
namespace Drupal\zencrm_entities\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Builds the form to delete Case entity type entities.
*/
class CaseEntityTypeDeleteForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.case_entity_type.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message(
$this->t('content @type: deleted @label.',
[
'@type' => $this->entity->bundle(),
'@label' => $this->entity->label(),
]
)
);
$form_state->setRedirectUrl($this->getCancelUrl());
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace Drupal\zencrm_entities\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Class CaseEntityTypeForm.
*/
class CaseEntityTypeForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$case_entity_type = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $case_entity_type->label(),
'#description' => $this->t("Label for the Case entity type."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $case_entity_type->id(),
'#machine_name' => [
'exists' => '\Drupal\zencrm_entities\Entity\CaseEntityType::load',
],
'#disabled' => !$case_entity_type->isNew(),
];
/* You will need additional form elements for your custom properties. */
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$case_entity_type = $this->entity;
$status = $case_entity_type->save();
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created the %label Case entity type.', [
'%label' => $case_entity_type->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label Case entity type.', [
'%label' => $case_entity_type->label(),
]));
}
$form_state->setRedirectUrl($case_entity_type->toUrl('collection'));
}
}