Changed name of Profile entity to Hat

so as not to conflict with Profile or Profile2 module on existing sites
This commit is contained in:
naomi
2018-04-11 12:18:27 +02:00
parent 1e1bfe0ca9
commit a48e37ad39
29 changed files with 1024 additions and 116 deletions

View File

@ -192,7 +192,7 @@ class ContactDetails extends ContentEntityBase implements ContactDetailsInterfac
// Person field is always set from the context so no form or display required.
$fields['person'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Person'))
->setDescription(t('The person this profile is of.'))
->setDescription(t('The person this set of contact details is for.'))
->setSetting('target_type', 'person')
->setRequired(TRUE);

View File

@ -0,0 +1,210 @@
<?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 Hat entity.
*
* @ingroup zencrm_entities
*
* @ContentEntityType(
* id = "hat",
* label = @Translation("Hat"),
* bundle_label = @Translation("Hat type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\zencrm_entities\HatListBuilder",
* "views_data" = "Drupal\zencrm_entities\Entity\HatViewsData",
* "translation" = "Drupal\zencrm_entities\HatTranslationHandler",
*
* "form" = {
* "default" = "Drupal\zencrm_entities\Form\HatForm",
* "add" = "Drupal\zencrm_entities\Form\HatForm",
* "edit" = "Drupal\zencrm_entities\Form\HatForm",
* "delete" = "Drupal\zencrm_entities\Form\HatDeleteForm",
* },
* "access" = "Drupal\zencrm_entities\HatAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\zencrm_entities\HatHtmlRouteProvider",
* },
* },
* base_table = "hat",
* data_table = "hat_field_data",
* translatable = TRUE,
* admin_permission = "administer hat entities",
* entity_keys = {
* "id" = "id",
* "bundle" = "type",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* },
* links = {
* "canonical" = "/admin/structure/hat/{hat}",
* "add-page" = "/admin/structure/hat/add",
* "add-form" = "/admin/structure/hat/add/{hat_type}",
* "edit-form" = "/admin/structure/hat/{hat}/edit",
* "delete-form" = "/admin/structure/hat/{hat}/delete",
* "collection" = "/admin/structure/hat",
* },
* bundle_entity_type = "hat_type",
* field_ui_base_route = "entity.hat_type.edit_form"
* )
*/
class Hat extends ContentEntityBase implements HatInterface {
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 the author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE);
# ->setDisplayOptions('form', [
# 'type' => 'entity_reference_autocomplete',
# 'weight' => 5,
# 'settings' => [
# 'match_operator' => 'CONTAINS',
# 'size' => '60',
# 'autocomplete_type' => 'tags',
# 'placeholder' => '',
# ],
# ]);
// This field is always implied from the context,
// so has no form or view display.
$fields['person'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Person'))
->setDescription(t('The person this hat is of.'))
->setSetting('target_type', 'person');
// This field is computed in a presave hook, and used for entity reference
// options when selecting a person for involvement in a case etc.
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of this hat instance as it appears in entity reference fields.'));
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Hat is published.'))
# ->setDisplayOptions('form', [
# 'type' => 'boolean_checkbox',
# 'weight' => -3,
# ])
->setDefaultValue(TRUE);
$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 Hat entities.
*
* @ingroup zencrm_entities
*/
interface HatInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface {
// Add get/set methods for your configuration properties here.
/**
* Gets the Hat name.
*
* @return string
* Name of the Hat.
*/
public function getName();
/**
* Sets the Hat name.
*
* @param string $name
* The Hat name.
*
* @return \Drupal\zencrm_entities\Entity\HatInterface
* The called Hat entity.
*/
public function setName($name);
/**
* Gets the Hat creation timestamp.
*
* @return int
* Creation timestamp of the Hat.
*/
public function getCreatedTime();
/**
* Sets the Hat creation timestamp.
*
* @param int $timestamp
* The Hat creation timestamp.
*
* @return \Drupal\zencrm_entities\Entity\HatInterface
* The called Hat entity.
*/
public function setCreatedTime($timestamp);
/**
* Returns the Hat published status indicator.
*
* Unpublished Hat are only visible to restricted users.
*
* @return bool
* TRUE if the Hat is published.
*/
public function isPublished();
/**
* Sets the published status of a Hat.
*
* @param bool $published
* TRUE to set this Hat to published, FALSE to set it to unpublished.
*
* @return \Drupal\zencrm_entities\Entity\HatInterface
* The called Hat 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 Hat type entity.
*
* @ConfigEntityType(
* id = "hat_type",
* label = @Translation("Hat type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\zencrm_entities\HatTypeListBuilder",
* "form" = {
* "add" = "Drupal\zencrm_entities\Form\HatTypeForm",
* "edit" = "Drupal\zencrm_entities\Form\HatTypeForm",
* "delete" = "Drupal\zencrm_entities\Form\HatTypeDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\zencrm_entities\HatTypeHtmlRouteProvider",
* },
* },
* config_prefix = "hat_type",
* admin_permission = "administer site configuration",
* bundle_of = "hat",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "canonical" = "/admin/structure/hat_type/{hat_type}",
* "add-form" = "/admin/structure/hat_type/add",
* "edit-form" = "/admin/structure/hat_type/{hat_type}/edit",
* "delete-form" = "/admin/structure/hat_type/{hat_type}/delete",
* "collection" = "/admin/structure/hat_type"
* }
* )
*/
class HatType extends ConfigEntityBundleBase implements HatTypeInterface {
/**
* The Hat type ID.
*
* @var string
*/
protected $id;
/**
* The Hat 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 Hat type entities.
*/
interface HatTypeInterface 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 Hat entities.
*/
class HatViewsData 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 Hat entities.
*
* @ingroup zencrm_entities
*/
class HatDeleteForm 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 Hat edit forms.
*
* @ingroup zencrm_entities
*/
class HatForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var $entity \Drupal\zencrm_entities\Entity\Hat */
$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 Hat.', [
'%label' => $entity->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label Hat.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.hat.canonical', ['hat' => $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 HatSettingsForm.
*
* @ingroup zencrm_entities
*/
class HatSettingsForm extends FormBase {
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'hat_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 Hat 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['hat_settings']['#markup'] = 'Settings form for Hat 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 Hat type entities.
*/
class HatTypeDeleteForm 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.hat_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 HatTypeForm.
*/
class HatTypeForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$hat_type = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $hat_type->label(),
'#description' => $this->t("Label for the Hat type."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $hat_type->id(),
'#machine_name' => [
'exists' => '\Drupal\zencrm_entities\Entity\HatType::load',
],
'#disabled' => !$hat_type->isNew(),
];
/* You will need additional form elements for your custom properties. */
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$hat_type = $this->entity;
$status = $hat_type->save();
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created the %label Hat type.', [
'%label' => $hat_type->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label Hat type.', [
'%label' => $hat_type->label(),
]));
}
$form_state->setRedirectUrl($hat_type->toUrl('collection'));
}
}

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 Hat entity.
*
* @see \Drupal\zencrm_entities\Entity\Hat.
*/
class HatAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\zencrm_entities\Entity\HatInterface $entity */
switch ($operation) {
case 'view':
if (!$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished hat entities');
}
return AccessResult::allowedIfHasPermission($account, 'view published hat entities');
case 'update':
return AccessResult::allowedIfHasPermission($account, 'edit hat entities');
case 'delete':
return AccessResult::allowedIfHasPermission($account, 'delete hat entities');
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add hat 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 Hat entities.
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class HatHtmlRouteProvider 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\HatSettingsForm',
'_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 Hat entities.
*
* @ingroup zencrm_entities
*/
class HatListBuilder extends EntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('Hat ID');
$header['name'] = $this->t('Name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var $entity \Drupal\zencrm_entities\Entity\Hat */
$row['id'] = $entity->id();
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.hat.edit_form',
['hat' => $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 hat.
*/
class HatTranslationHandler 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 Hat type entities.
*
* @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class HatTypeHtmlRouteProvider 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 Hat type entities.
*/
class HatTypeListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Hat 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);
}
}