commit 4756074cf733152c7b2b8572dbde8ebeab7e6eeb Author: naomi Date: Wed Apr 4 12:59:01 2018 +0200 Generated module and Person entity Changed "published" status of entity to "enabled". Removed author field from display diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..70d917a --- /dev/null +++ b/composer.json @@ -0,0 +1,14 @@ +{ + "name": "drupal/zencrm", + "type": "drupal-module", + "description": "Zen CRM", + "keywords": ["Drupal"], + "license": "GPL-2.0+", + "homepage": "https://www.drupal.org/project/zencrm", + "minimum-stability": "dev", + "support": { + "issues": "https://www.drupal.org/project/issues/zencrm", + "source": "http://cgit.drupalcode.org/zencrm" + }, + "require": { } +} diff --git a/modules/zencrm_entities/composer.json b/modules/zencrm_entities/composer.json new file mode 100644 index 0000000..d587368 --- /dev/null +++ b/modules/zencrm_entities/composer.json @@ -0,0 +1,14 @@ +{ + "name": "drupal/zencrm_entities", + "type": "drupal-module", + "description": "Zen CRM Entities", + "keywords": ["Drupal"], + "license": "GPL-2.0+", + "homepage": "https://www.drupal.org/project/zencrm_entities", + "minimum-stability": "dev", + "support": { + "issues": "https://www.drupal.org/project/issues/zencrm_entities", + "source": "http://cgit.drupalcode.org/zencrm_entities" + }, + "require": { } +} diff --git a/modules/zencrm_entities/person.page.inc b/modules/zencrm_entities/person.page.inc new file mode 100644 index 0000000..7230a19 --- /dev/null +++ b/modules/zencrm_entities/person.page.inc @@ -0,0 +1,30 @@ +entityManager()->getStorage('person')->loadRevision($person_revision); + $view_builder = $this->entityManager()->getViewBuilder('person'); + + return $view_builder->view($person); + } + + /** + * Page title callback for a Person revision. + * + * @param int $person_revision + * The Person revision ID. + * + * @return string + * The page title. + */ + public function revisionPageTitle($person_revision) { + $person = $this->entityManager()->getStorage('person')->loadRevision($person_revision); + return $this->t('Revision of %title from %date', ['%title' => $person->label(), '%date' => format_date($person->getRevisionCreationTime())]); + } + + /** + * Generates an overview table of older revisions of a Person . + * + * @param \Drupal\zencrm_entities\Entity\PersonInterface $person + * A Person object. + * + * @return array + * An array as expected by drupal_render(). + */ + public function revisionOverview(PersonInterface $person) { + $account = $this->currentUser(); + $langcode = $person->language()->getId(); + $langname = $person->language()->getName(); + $languages = $person->getTranslationLanguages(); + $has_translations = (count($languages) > 1); + $person_storage = $this->entityManager()->getStorage('person'); + + $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $person->label()]) : $this->t('Revisions for %title', ['%title' => $person->label()]); + $header = [$this->t('Revision'), $this->t('Operations')]; + + $revert_permission = (($account->hasPermission("revert all person revisions") || $account->hasPermission('administer person entities'))); + $delete_permission = (($account->hasPermission("delete all person revisions") || $account->hasPermission('administer person entities'))); + + $rows = []; + + $vids = $person_storage->revisionIds($person); + + $latest_revision = TRUE; + + foreach (array_reverse($vids) as $vid) { + /** @var \Drupal\zencrm_entities\PersonInterface $revision */ + $revision = $person_storage->loadRevision($vid); + // Only show revisions that are affected by the language that is being + // displayed. + if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) { + $username = [ + '#theme' => 'username', + '#account' => $revision->getRevisionUser(), + ]; + + // Use revision link to link to revisions that are not active. + $date = \Drupal::service('date.formatter')->format($revision->getRevisionCreationTime(), 'short'); + if ($vid != $person->getRevisionId()) { + $link = $this->l($date, new Url('entity.person.revision', ['person' => $person->id(), 'person_revision' => $vid])); + } + else { + $link = $person->link($date); + } + + $row = []; + $column = [ + 'data' => [ + '#type' => 'inline_template', + '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}

{{ message }}

{% endif %}', + '#context' => [ + 'date' => $link, + 'username' => \Drupal::service('renderer')->renderPlain($username), + 'message' => ['#markup' => $revision->getRevisionLogMessage(), '#allowed_tags' => Xss::getHtmlTagList()], + ], + ], + ]; + $row[] = $column; + + if ($latest_revision) { + $row[] = [ + 'data' => [ + '#prefix' => '', + '#markup' => $this->t('Current revision'), + '#suffix' => '', + ], + ]; + foreach ($row as &$current) { + $current['class'] = ['revision-current']; + } + $latest_revision = FALSE; + } + else { + $links = []; + if ($revert_permission) { + $links['revert'] = [ + 'title' => $this->t('Revert'), + 'url' => $has_translations ? + Url::fromRoute('entity.person.translation_revert', ['person' => $person->id(), 'person_revision' => $vid, 'langcode' => $langcode]) : + Url::fromRoute('entity.person.revision_revert', ['person' => $person->id(), 'person_revision' => $vid]), + ]; + } + + if ($delete_permission) { + $links['delete'] = [ + 'title' => $this->t('Delete'), + 'url' => Url::fromRoute('entity.person.revision_delete', ['person' => $person->id(), 'person_revision' => $vid]), + ]; + } + + $row[] = [ + 'data' => [ + '#type' => 'operations', + '#links' => $links, + ], + ]; + } + + $rows[] = $row; + } + } + + $build['person_revisions_table'] = [ + '#theme' => 'table', + '#rows' => $rows, + '#header' => $header, + ]; + + return $build; + } + +} diff --git a/modules/zencrm_entities/src/Entity/Person.php b/modules/zencrm_entities/src/Entity/Person.php new file mode 100644 index 0000000..fd38dee --- /dev/null +++ b/modules/zencrm_entities/src/Entity/Person.php @@ -0,0 +1,288 @@ + \Drupal::currentUser()->id(), + ]; + } + + /** + * {@inheritdoc} + */ + protected function urlRouteParameters($rel) { + $uri_route_parameters = parent::urlRouteParameters($rel); + + if ($rel === 'revision_revert' && $this instanceof RevisionableInterface) { + $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId(); + } + elseif ($rel === 'revision_delete' && $this instanceof RevisionableInterface) { + $uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId(); + } + + return $uri_route_parameters; + } + + /** + * {@inheritdoc} + */ + public function preSave(EntityStorageInterface $storage) { + parent::preSave($storage); + + foreach (array_keys($this->getTranslationLanguages()) as $langcode) { + $translation = $this->getTranslation($langcode); + + // If no owner has been set explicitly, make the anonymous user the owner. + if (!$translation->getOwner()) { + $translation->setOwnerId(0); + } + } + + // If no revision author has been set explicitly, make the person owner the + // revision author. + if (!$this->getRevisionUser()) { + $this->setRevisionUserId($this->getOwnerId()); + } + } + + + /** + * {@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 Person entity.')) + ->setRevisionable(TRUE) + ->setSetting('target_type', 'user') + ->setSetting('handler', 'default') + ->setTranslatable(TRUE); + + $fields['first_name'] = BaseFieldDefinition::create('string') + ->setLabel(t('First Name')) + ->setDescription(t('First Name.')) + ->setRevisionable(TRUE) + ->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['middle_names'] = BaseFieldDefinition::create('string') + ->setLabel(t('Middle Names')) + ->setDescription(t('Middle Names.')) + ->setRevisionable(TRUE) + ->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); + + $fields['last_name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Last Name')) + ->setDescription(t('Last Name.')) + ->setRevisionable(TRUE) + ->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('Enabled')) + ->setDescription(t('If this is ticked, the record is active')) + ->setRevisionable(TRUE) + ->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.')); + + $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean') + ->setLabel(t('Revision translation affected')) + ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.')) + ->setReadOnly(TRUE) + ->setRevisionable(TRUE) + ->setTranslatable(TRUE); + + return $fields; + } + +} diff --git a/modules/zencrm_entities/src/Entity/PersonInterface.php b/modules/zencrm_entities/src/Entity/PersonInterface.php new file mode 100644 index 0000000..ebe7d5f --- /dev/null +++ b/modules/zencrm_entities/src/Entity/PersonInterface.php @@ -0,0 +1,97 @@ +entity->isNew()) { + $form['new_revision'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Create new revision'), + '#default_value' => FALSE, + '#weight' => 10, + ]; + } + + $entity = $this->entity; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $entity = $this->entity; + + // Save as a new revision if requested to do so. + if (!$form_state->isValueEmpty('new_revision') && $form_state->getValue('new_revision') != FALSE) { + $entity->setNewRevision(); + + // If a new revision is created, save the current user as revision author. + $entity->setRevisionCreationTime(REQUEST_TIME); + $entity->setRevisionUserId(\Drupal::currentUser()->id()); + } + else { + $entity->setNewRevision(FALSE); + } + + $status = parent::save($form, $form_state); + + switch ($status) { + case SAVED_NEW: + drupal_set_message($this->t('Created the %label Person.', [ + '%label' => $entity->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label Person.', [ + '%label' => $entity->label(), + ])); + } + $form_state->setRedirect('entity.person.canonical', ['person' => $entity->id()]); + } + +} diff --git a/modules/zencrm_entities/src/Form/PersonRevisionDeleteForm.php b/modules/zencrm_entities/src/Form/PersonRevisionDeleteForm.php new file mode 100644 index 0000000..c6addc9 --- /dev/null +++ b/modules/zencrm_entities/src/Form/PersonRevisionDeleteForm.php @@ -0,0 +1,123 @@ +PersonStorage = $entity_storage; + $this->connection = $connection; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + $entity_manager = $container->get('entity.manager'); + return new static( + $entity_manager->getStorage('person'), + $container->get('database') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'person_revision_delete_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => format_date($this->revision->getRevisionCreationTime())]); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return new Url('entity.person.version_history', ['person' => $this->revision->id()]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $person_revision = NULL) { + $this->revision = $this->PersonStorage->loadRevision($person_revision); + $form = parent::buildForm($form, $form_state); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->PersonStorage->deleteRevision($this->revision->getRevisionId()); + + $this->logger('content')->notice('Person: deleted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + drupal_set_message(t('Revision from %revision-date of Person %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()])); + $form_state->setRedirect( + 'entity.person.canonical', + ['person' => $this->revision->id()] + ); + if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {person_field_revision} WHERE id = :id', [':id' => $this->revision->id()])->fetchField() > 1) { + $form_state->setRedirect( + 'entity.person.version_history', + ['person' => $this->revision->id()] + ); + } + } + +} diff --git a/modules/zencrm_entities/src/Form/PersonRevisionRevertForm.php b/modules/zencrm_entities/src/Form/PersonRevisionRevertForm.php new file mode 100644 index 0000000..25ad7e5 --- /dev/null +++ b/modules/zencrm_entities/src/Form/PersonRevisionRevertForm.php @@ -0,0 +1,149 @@ +PersonStorage = $entity_storage; + $this->dateFormatter = $date_formatter; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorage('person'), + $container->get('date.formatter') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'person_revision_revert_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return new Url('entity.person.version_history', ['person' => $this->revision->id()]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return t('Revert'); + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return ''; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $person_revision = NULL) { + $this->revision = $this->PersonStorage->loadRevision($person_revision); + $form = parent::buildForm($form, $form_state); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // The revision timestamp will be updated when the revision is saved. Keep + // the original one for the confirmation message. + $original_revision_timestamp = $this->revision->getRevisionCreationTime(); + + $this->revision = $this->prepareRevertedRevision($this->revision, $form_state); + $this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]); + $this->revision->save(); + + $this->logger('content')->notice('Person: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + drupal_set_message(t('Person %title has been reverted to the revision from %revision-date.', ['%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)])); + $form_state->setRedirect( + 'entity.person.version_history', + ['person' => $this->revision->id()] + ); + } + + /** + * Prepares a revision to be reverted. + * + * @param \Drupal\zencrm_entities\Entity\PersonInterface $revision + * The revision to be reverted. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return \Drupal\zencrm_entities\Entity\PersonInterface + * The prepared revision ready to be stored. + */ + protected function prepareRevertedRevision(PersonInterface $revision, FormStateInterface $form_state) { + $revision->setNewRevision(); + $revision->isDefaultRevision(TRUE); + $revision->setRevisionCreationTime(REQUEST_TIME); + + return $revision; + } + +} diff --git a/modules/zencrm_entities/src/Form/PersonRevisionRevertTranslationForm.php b/modules/zencrm_entities/src/Form/PersonRevisionRevertTranslationForm.php new file mode 100644 index 0000000..2a5aa67 --- /dev/null +++ b/modules/zencrm_entities/src/Form/PersonRevisionRevertTranslationForm.php @@ -0,0 +1,115 @@ +languageManager = $language_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorage('person'), + $container->get('date.formatter'), + $container->get('language_manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'person_revision_revert_translation_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return t('Are you sure you want to revert @language translation to the revision from %revision-date?', ['@language' => $this->languageManager->getLanguageName($this->langcode), '%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $person_revision = NULL, $langcode = NULL) { + $this->langcode = $langcode; + $form = parent::buildForm($form, $form_state, $person_revision); + + $form['revert_untranslated_fields'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Revert content shared among translations'), + '#default_value' => FALSE, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + protected function prepareRevertedRevision(PersonInterface $revision, FormStateInterface $form_state) { + $revert_untranslated_fields = $form_state->getValue('revert_untranslated_fields'); + + /** @var \Drupal\zencrm_entities\Entity\PersonInterface $default_revision */ + $latest_revision = $this->PersonStorage->load($revision->id()); + $latest_revision_translation = $latest_revision->getTranslation($this->langcode); + + $revision_translation = $revision->getTranslation($this->langcode); + + foreach ($latest_revision_translation->getFieldDefinitions() as $field_name => $definition) { + if ($definition->isTranslatable() || $revert_untranslated_fields) { + $latest_revision_translation->set($field_name, $revision_translation->get($field_name)->getValue()); + } + } + + $latest_revision_translation->setNewRevision(); + $latest_revision_translation->isDefaultRevision(TRUE); + $revision->setRevisionCreationTime(REQUEST_TIME); + + return $latest_revision_translation; + } + +} diff --git a/modules/zencrm_entities/src/Form/PersonSettingsForm.php b/modules/zencrm_entities/src/Form/PersonSettingsForm.php new file mode 100644 index 0000000..402a0da --- /dev/null +++ b/modules/zencrm_entities/src/Form/PersonSettingsForm.php @@ -0,0 +1,53 @@ +isPublished()) { + return AccessResult::allowedIfHasPermission($account, 'view unpublished person entities'); + } + return AccessResult::allowedIfHasPermission($account, 'view published person entities'); + + case 'update': + return AccessResult::allowedIfHasPermission($account, 'edit person entities'); + + case 'delete': + return AccessResult::allowedIfHasPermission($account, 'delete person entities'); + } + + // Unknown operation, no opinion. + return AccessResult::neutral(); + } + + /** + * {@inheritdoc} + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + return AccessResult::allowedIfHasPermission($account, 'add person entities'); + } + +} diff --git a/modules/zencrm_entities/src/PersonHtmlRouteProvider.php b/modules/zencrm_entities/src/PersonHtmlRouteProvider.php new file mode 100644 index 0000000..8d9e7cb --- /dev/null +++ b/modules/zencrm_entities/src/PersonHtmlRouteProvider.php @@ -0,0 +1,196 @@ +id(); + + if ($history_route = $this->getHistoryRoute($entity_type)) { + $collection->add("entity.{$entity_type_id}.version_history", $history_route); + } + + if ($revision_route = $this->getRevisionRoute($entity_type)) { + $collection->add("entity.{$entity_type_id}.revision", $revision_route); + } + + if ($revert_route = $this->getRevisionRevertRoute($entity_type)) { + $collection->add("entity.{$entity_type_id}.revision_revert", $revert_route); + } + + if ($delete_route = $this->getRevisionDeleteRoute($entity_type)) { + $collection->add("entity.{$entity_type_id}.revision_delete", $delete_route); + } + + if ($translation_route = $this->getRevisionTranslationRevertRoute($entity_type)) { + $collection->add("{$entity_type_id}.revision_revert_translation_confirm", $translation_route); + } + + if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) { + $collection->add("$entity_type_id.settings", $settings_form_route); + } + + return $collection; + } + + /** + * Gets the version history route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getHistoryRoute(EntityTypeInterface $entity_type) { + if ($entity_type->hasLinkTemplate('version-history')) { + $route = new Route($entity_type->getLinkTemplate('version-history')); + $route + ->setDefaults([ + '_title' => "{$entity_type->getLabel()} revisions", + '_controller' => '\Drupal\zencrm_entities\Controller\PersonController::revisionOverview', + ]) + ->setRequirement('_permission', 'access person revisions') + ->setOption('_admin_route', TRUE); + + return $route; + } + } + + /** + * Gets the revision route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getRevisionRoute(EntityTypeInterface $entity_type) { + if ($entity_type->hasLinkTemplate('revision')) { + $route = new Route($entity_type->getLinkTemplate('revision')); + $route + ->setDefaults([ + '_controller' => '\Drupal\zencrm_entities\Controller\PersonController::revisionShow', + '_title_callback' => '\Drupal\zencrm_entities\Controller\PersonController::revisionPageTitle', + ]) + ->setRequirement('_permission', 'access person revisions') + ->setOption('_admin_route', TRUE); + + return $route; + } + } + + /** + * Gets the revision revert route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getRevisionRevertRoute(EntityTypeInterface $entity_type) { + if ($entity_type->hasLinkTemplate('revision_revert')) { + $route = new Route($entity_type->getLinkTemplate('revision_revert')); + $route + ->setDefaults([ + '_form' => '\Drupal\zencrm_entities\Form\PersonRevisionRevertForm', + '_title' => 'Revert to earlier revision', + ]) + ->setRequirement('_permission', 'revert all person revisions') + ->setOption('_admin_route', TRUE); + + return $route; + } + } + + /** + * Gets the revision delete route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getRevisionDeleteRoute(EntityTypeInterface $entity_type) { + if ($entity_type->hasLinkTemplate('revision_delete')) { + $route = new Route($entity_type->getLinkTemplate('revision_delete')); + $route + ->setDefaults([ + '_form' => '\Drupal\zencrm_entities\Form\PersonRevisionDeleteForm', + '_title' => 'Delete earlier revision', + ]) + ->setRequirement('_permission', 'delete all person revisions') + ->setOption('_admin_route', TRUE); + + return $route; + } + } + + /** + * Gets the revision translation revert route. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * + * @return \Symfony\Component\Routing\Route|null + * The generated route, if available. + */ + protected function getRevisionTranslationRevertRoute(EntityTypeInterface $entity_type) { + if ($entity_type->hasLinkTemplate('translation_revert')) { + $route = new Route($entity_type->getLinkTemplate('translation_revert')); + $route + ->setDefaults([ + '_form' => '\Drupal\zencrm_entities\Form\PersonRevisionRevertTranslationForm', + '_title' => 'Revert to earlier revision of a translation', + ]) + ->setRequirement('_permission', 'revert all person revisions') + ->setOption('_admin_route', TRUE); + + return $route; + } + } + + /** + * 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\PersonSettingsForm', + '_title' => "{$entity_type->getLabel()} settings", + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/modules/zencrm_entities/src/PersonListBuilder.php b/modules/zencrm_entities/src/PersonListBuilder.php new file mode 100644 index 0000000..e462a15 --- /dev/null +++ b/modules/zencrm_entities/src/PersonListBuilder.php @@ -0,0 +1,40 @@ +t('Person ID'); + $header['name'] = $this->t('Name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + /* @var $entity \Drupal\zencrm_entities\Entity\Person */ + $row['id'] = $entity->id(); + $row['name'] = Link::createFromRoute( + $entity->label(), + 'entity.person.edit_form', + ['person' => $entity->id()] + ); + return $row + parent::buildRow($entity); + } + +} diff --git a/modules/zencrm_entities/src/PersonStorage.php b/modules/zencrm_entities/src/PersonStorage.php new file mode 100644 index 0000000..84a3340 --- /dev/null +++ b/modules/zencrm_entities/src/PersonStorage.php @@ -0,0 +1,58 @@ +database->query( + 'SELECT vid FROM {person_revision} WHERE id=:id ORDER BY vid', + [':id' => $entity->id()] + )->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function userRevisionIds(AccountInterface $account) { + return $this->database->query( + 'SELECT vid FROM {person_field_revision} WHERE uid = :uid ORDER BY vid', + [':uid' => $account->id()] + )->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function countDefaultLanguageRevisions(PersonInterface $entity) { + return $this->database->query('SELECT COUNT(*) FROM {person_field_revision} WHERE id = :id AND default_langcode = 1', [':id' => $entity->id()]) + ->fetchField(); + } + + /** + * {@inheritdoc} + */ + public function clearRevisionsLanguage(LanguageInterface $language) { + return $this->database->update('person_revision') + ->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]) + ->condition('langcode', $language->getId()) + ->execute(); + } + +} diff --git a/modules/zencrm_entities/src/PersonStorageInterface.php b/modules/zencrm_entities/src/PersonStorageInterface.php new file mode 100644 index 0000000..727393c --- /dev/null +++ b/modules/zencrm_entities/src/PersonStorageInterface.php @@ -0,0 +1,61 @@ + + {% if content %} + {{- content -}} + {% endif %} + diff --git a/modules/zencrm_entities/templates/zencrm-entities.html.twig b/modules/zencrm_entities/templates/zencrm-entities.html.twig new file mode 100644 index 0000000..91e43c8 --- /dev/null +++ b/modules/zencrm_entities/templates/zencrm-entities.html.twig @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/modules/zencrm_entities/tests/src/Functional/LoadTest.php b/modules/zencrm_entities/tests/src/Functional/LoadTest.php new file mode 100644 index 0000000..2b23546 --- /dev/null +++ b/modules/zencrm_entities/tests/src/Functional/LoadTest.php @@ -0,0 +1,46 @@ +user = $this->drupalCreateUser(['administer site configuration']); + $this->drupalLogin($this->user); + } + + /** + * Tests that the home page loads with a 200 response. + */ + public function testLoad() { + $this->drupalGet(Url::fromRoute('')); + $this->assertSession()->statusCodeEquals(200); + } + +} diff --git a/modules/zencrm_entities/zencrm_entities.info.yml b/modules/zencrm_entities/zencrm_entities.info.yml new file mode 100644 index 0000000..7e46fd5 --- /dev/null +++ b/modules/zencrm_entities/zencrm_entities.info.yml @@ -0,0 +1,5 @@ +name: 'Zen CRM Entities' +type: module +description: 'Zen CRM Entities' +core: 8.x +package: 'Zen CRM' diff --git a/modules/zencrm_entities/zencrm_entities.links.action.yml b/modules/zencrm_entities/zencrm_entities.links.action.yml new file mode 100644 index 0000000..13b90f7 --- /dev/null +++ b/modules/zencrm_entities/zencrm_entities.links.action.yml @@ -0,0 +1,5 @@ +entity.person.add_form: + route_name: entity.person.add_form + title: 'Add Person' + appears_on: + - entity.person.collection diff --git a/modules/zencrm_entities/zencrm_entities.links.menu.yml b/modules/zencrm_entities/zencrm_entities.links.menu.yml new file mode 100644 index 0000000..e55e04e --- /dev/null +++ b/modules/zencrm_entities/zencrm_entities.links.menu.yml @@ -0,0 +1,14 @@ + +# Person menu items definition +entity.person.collection: + title: 'Person list' + route_name: entity.person.collection + description: 'List Person entities' + parent: system.admin_structure + weight: 100 + +person.admin.structure.settings: + title: 'Person settings' + description: 'Configure Person entities' + route_name: person.settings + parent: system.admin_structure diff --git a/modules/zencrm_entities/zencrm_entities.links.task.yml b/modules/zencrm_entities/zencrm_entities.links.task.yml new file mode 100644 index 0000000..b0f95d1 --- /dev/null +++ b/modules/zencrm_entities/zencrm_entities.links.task.yml @@ -0,0 +1,27 @@ +# Person routing definition +person.settings_tab: + route_name: person.settings + title: 'Settings' + base_route: person.settings + +entity.person.canonical: + route_name: entity.person.canonical + base_route: entity.person.canonical + title: 'View' + +entity.person.edit_form: + route_name: entity.person.edit_form + base_route: entity.person.canonical + title: 'Edit' + +entity.person.version_history: + route_name: entity.person.version_history + base_route: entity.person.canonical + title: 'Revisions' + +entity.person.delete_form: + route_name: entity.person.delete_form + base_route: entity.person.canonical + title: Delete + weight: 10 + diff --git a/modules/zencrm_entities/zencrm_entities.module b/modules/zencrm_entities/zencrm_entities.module new file mode 100644 index 0000000..be031b1 --- /dev/null +++ b/modules/zencrm_entities/zencrm_entities.module @@ -0,0 +1,35 @@ +' . t('About') . ''; + $output .= '

' . t('Zen CRM Entities') . '

'; + return $output; + + default: + } +} + +/** + * Implements hook_theme(). + */ +function zencrm_entities_theme() { + return [ + 'zencrm_entities' => [ + 'render element' => 'children', + ], + ]; +} diff --git a/modules/zencrm_entities/zencrm_entities.permissions.yml b/modules/zencrm_entities/zencrm_entities.permissions.yml new file mode 100644 index 0000000..9e60c2f --- /dev/null +++ b/modules/zencrm_entities/zencrm_entities.permissions.yml @@ -0,0 +1,30 @@ +add person entities: + title: 'Create new Person entities' + +administer person entities: + title: 'Administer Person entities' + description: 'Allow to access the administration form to configure Person entities.' + restrict access: true + +delete person entities: + title: 'Delete Person entities' + +edit person entities: + title: 'Edit Person entities' + +view published person entities: + title: 'View published Person entities' + +view unpublished person entities: + title: 'View unpublished Person entities' + +view all person revisions: + title: 'View all Person revisions' + +revert all person revisions: + title: 'Revert all Person revisions' + description: 'Role requires permission view Person revisions and edit rights for person entities in question or administer person entities.' + +delete all person revisions: + title: 'Delete all revisions' + description: 'Role requires permission to view Person revisions and delete rights for person entities in question or administer person entities.' diff --git a/templates/zencrm.html.twig b/templates/zencrm.html.twig new file mode 100644 index 0000000..91e43c8 --- /dev/null +++ b/templates/zencrm.html.twig @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/src/Functional/LoadTest.php b/tests/src/Functional/LoadTest.php new file mode 100644 index 0000000..b3c682b --- /dev/null +++ b/tests/src/Functional/LoadTest.php @@ -0,0 +1,46 @@ +user = $this->drupalCreateUser(['administer site configuration']); + $this->drupalLogin($this->user); + } + + /** + * Tests that the home page loads with a 200 response. + */ + public function testLoad() { + $this->drupalGet(Url::fromRoute('')); + $this->assertSession()->statusCodeEquals(200); + } + +} diff --git a/zencrm.info.yml b/zencrm.info.yml new file mode 100644 index 0000000..de8354d --- /dev/null +++ b/zencrm.info.yml @@ -0,0 +1,5 @@ +name: 'Zen CRM' +type: module +description: 'Zen CRM' +core: 8.x +package: 'Zen CRM' diff --git a/zencrm.module b/zencrm.module new file mode 100644 index 0000000..20421e4 --- /dev/null +++ b/zencrm.module @@ -0,0 +1,35 @@ +' . t('About') . ''; + $output .= '

' . t('Zen CRM') . '

'; + return $output; + + default: + } +} + +/** + * Implements hook_theme(). + */ +function zencrm_theme() { + return [ + 'zencrm' => [ + 'render element' => 'children', + ], + ]; +}