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 %} +