diff --git a/modules/zencrm_entities/case_entity.page.inc b/modules/zencrm_entities/case_entity.page.inc new file mode 100644 index 0000000..e78e68e --- /dev/null +++ b/modules/zencrm_entities/case_entity.page.inc @@ -0,0 +1,30 @@ +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'); + } + +} diff --git a/modules/zencrm_entities/src/CaseEntityHtmlRouteProvider.php b/modules/zencrm_entities/src/CaseEntityHtmlRouteProvider.php new file mode 100644 index 0000000..cf151ad --- /dev/null +++ b/modules/zencrm_entities/src/CaseEntityHtmlRouteProvider.php @@ -0,0 +1,56 @@ +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; + } + } + +} diff --git a/modules/zencrm_entities/src/CaseEntityListBuilder.php b/modules/zencrm_entities/src/CaseEntityListBuilder.php new file mode 100644 index 0000000..d6c1bd2 --- /dev/null +++ b/modules/zencrm_entities/src/CaseEntityListBuilder.php @@ -0,0 +1,40 @@ +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); + } + +} diff --git a/modules/zencrm_entities/src/CaseEntityTranslationHandler.php b/modules/zencrm_entities/src/CaseEntityTranslationHandler.php new file mode 100644 index 0000000..37f20bb --- /dev/null +++ b/modules/zencrm_entities/src/CaseEntityTranslationHandler.php @@ -0,0 +1,14 @@ +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); + } + +} diff --git a/modules/zencrm_entities/src/Entity/CaseEntity.php b/modules/zencrm_entities/src/Entity/CaseEntity.php new file mode 100644 index 0000000..68d2489 --- /dev/null +++ b/modules/zencrm_entities/src/Entity/CaseEntity.php @@ -0,0 +1,224 @@ + \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; + } + +} diff --git a/modules/zencrm_entities/src/Entity/CaseEntityInterface.php b/modules/zencrm_entities/src/Entity/CaseEntityInterface.php new file mode 100644 index 0000000..7521202 --- /dev/null +++ b/modules/zencrm_entities/src/Entity/CaseEntityInterface.php @@ -0,0 +1,77 @@ +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()]); + } + +} diff --git a/modules/zencrm_entities/src/Form/CaseEntitySettingsForm.php b/modules/zencrm_entities/src/Form/CaseEntitySettingsForm.php new file mode 100644 index 0000000..507c6f1 --- /dev/null +++ b/modules/zencrm_entities/src/Form/CaseEntitySettingsForm.php @@ -0,0 +1,53 @@ +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()); + } + +} diff --git a/modules/zencrm_entities/src/Form/CaseEntityTypeForm.php b/modules/zencrm_entities/src/Form/CaseEntityTypeForm.php new file mode 100644 index 0000000..783b980 --- /dev/null +++ b/modules/zencrm_entities/src/Form/CaseEntityTypeForm.php @@ -0,0 +1,65 @@ +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')); + } + +} diff --git a/modules/zencrm_entities/templates/case-entity-content-add-list.html.twig b/modules/zencrm_entities/templates/case-entity-content-add-list.html.twig new file mode 100644 index 0000000..2dbb6a8 --- /dev/null +++ b/modules/zencrm_entities/templates/case-entity-content-add-list.html.twig @@ -0,0 +1,23 @@ +{# +/** + * @file + * Default theme implementation to present a list of custom content entity types/bundles. + * + * Available variables: + * - types: A collection of all the available custom entity types/bundles. + * Each type/bundle contains the following: + * - link: A link to add a content entity of this type. + * - description: A description of this content entity types/bundle. + * + * @see template_preprocess_case_entity_content_add_list() + * + * @ingroup themeable + */ +#} +{% spaceless %} +
+ {% for type in types %} +
{{ type.link }}
+ {% endfor %} +
+{% endspaceless %} diff --git a/modules/zencrm_entities/templates/case_entity.html.twig b/modules/zencrm_entities/templates/case_entity.html.twig new file mode 100644 index 0000000..41f7a76 --- /dev/null +++ b/modules/zencrm_entities/templates/case_entity.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file case_entity.html.twig + * Default theme implementation to present Case entity data. + * + * This template is used when viewing Case entity 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_case_entity() + * + * @ingroup themeable + */ +#} + + {% if content %} + {{- content -}} + {% endif %} + diff --git a/modules/zencrm_entities/zencrm_entities.links.action.yml b/modules/zencrm_entities/zencrm_entities.links.action.yml index 4c53da0..20bba41 100644 --- a/modules/zencrm_entities/zencrm_entities.links.action.yml +++ b/modules/zencrm_entities/zencrm_entities.links.action.yml @@ -30,3 +30,14 @@ entity.hat_type.add_form: appears_on: - entity.hat_type.collection +entity.case_entity.add_form: + route_name: entity.case_entity.add_page + title: 'Add Case entity' + appears_on: + - entity.case_entity.collection +entity.case_entity_type.add_form: + route_name: entity.case_entity_type.add_form + title: 'Add Case entity type' + appears_on: + - entity.case_entity_type.collection + diff --git a/modules/zencrm_entities/zencrm_entities.links.menu.yml b/modules/zencrm_entities/zencrm_entities.links.menu.yml index fbbedb5..60664c4 100644 --- a/modules/zencrm_entities/zencrm_entities.links.menu.yml +++ b/modules/zencrm_entities/zencrm_entities.links.menu.yml @@ -72,3 +72,21 @@ entity.hat_type.collection: parent: system.admin_structure weight: 99 + +# Case entity menu items definition +entity.case_entity.collection: + title: 'Case entity list' + route_name: entity.case_entity.collection + description: 'List Case entity entities' + parent: system.admin_structure + weight: 100 + + +# Case entity type menu items definition +entity.case_entity_type.collection: + title: 'Case entity type' + route_name: entity.case_entity_type.collection + description: 'List Case entity type (bundles)' + parent: system.admin_structure + weight: 99 + diff --git a/modules/zencrm_entities/zencrm_entities.links.task.yml b/modules/zencrm_entities/zencrm_entities.links.task.yml index 0545d15..b21d97e 100644 --- a/modules/zencrm_entities/zencrm_entities.links.task.yml +++ b/modules/zencrm_entities/zencrm_entities.links.task.yml @@ -104,3 +104,21 @@ entity.hat.delete_form: title: Delete weight: 10 +# Case entity routing definition + +entity.case_entity.canonical: + route_name: entity.case_entity.canonical + base_route: entity.case_entity.canonical + title: 'View' + +entity.case_entity.edit_form: + route_name: entity.case_entity.edit_form + base_route: entity.case_entity.canonical + title: 'Edit' + +entity.case_entity.delete_form: + route_name: entity.case_entity.delete_form + base_route: entity.case_entity.canonical + title: Delete + weight: 10 + diff --git a/modules/zencrm_entities/zencrm_entities.module b/modules/zencrm_entities/zencrm_entities.module index 7228358..2d142af 100644 --- a/modules/zencrm_entities/zencrm_entities.module +++ b/modules/zencrm_entities/zencrm_entities.module @@ -71,6 +71,16 @@ function zencrm_entities_theme() { 'variables' => ['content' => NULL], 'file' => 'hat.page.inc', ]; + $theme['case_entity'] = [ + 'render element' => 'elements', + 'file' => 'case_entity.page.inc', + 'template' => 'case_entity', + ]; + $theme['case_entity_content_add_list'] = [ + 'render element' => 'content', + 'variables' => ['content' => NULL], + 'file' => 'case_entity.page.inc', + ]; return $theme; } @@ -89,3 +99,19 @@ function zencrm_entities_theme_suggestions_hat(array $variables) { $suggestions[] = 'hat__' . $entity->id() . '__' . $sanitized_view_mode; return $suggestions; } + +/** +* Implements hook_theme_suggestions_HOOK(). +*/ +function zencrm_entities_theme_suggestions_case_entity(array $variables) { + $suggestions = []; + $entity = $variables['elements']['#case_entity']; + $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); + + $suggestions[] = 'case_entity__' . $sanitized_view_mode; + $suggestions[] = 'case_entity__' . $entity->bundle(); + $suggestions[] = 'case_entity__' . $entity->bundle() . '__' . $sanitized_view_mode; + $suggestions[] = 'case_entity__' . $entity->id(); + $suggestions[] = 'case_entity__' . $entity->id() . '__' . $sanitized_view_mode; + return $suggestions; +} diff --git a/modules/zencrm_entities/zencrm_entities.permissions.yml b/modules/zencrm_entities/zencrm_entities.permissions.yml index 486c58d..90c6ab4 100644 --- a/modules/zencrm_entities/zencrm_entities.permissions.yml +++ b/modules/zencrm_entities/zencrm_entities.permissions.yml @@ -116,3 +116,22 @@ view published hat entities: view unpublished hat entities: title: 'View unpublished Hat entities' +add case entity entities: + title: 'Create new Case entity entities' + +administer case entity entities: + title: 'Administer Case entity entities' + description: 'Allow to access the administration form to configure Case entity entities.' + restrict access: true + +delete case entity entities: + title: 'Delete Case entity entities' + +edit case entity entities: + title: 'Edit Case entity entities' + +view published case entity entities: + title: 'View published Case entity entities' + +view unpublished case entity entities: + title: 'View unpublished Case entity entities'