diff --git a/modules/zencrm_entities/config/optional/taxonomy.vocabulary.contact_types.yml b/modules/zencrm_entities/config/optional/taxonomy.vocabulary.contact_types.yml new file mode 100644 index 0000000..ec2ce9f --- /dev/null +++ b/modules/zencrm_entities/config/optional/taxonomy.vocabulary.contact_types.yml @@ -0,0 +1,8 @@ +langcode: en +status: true +dependencies: { } +name: 'Contact Types' +vid: contact_types +description: '' +hierarchy: 0 +weight: 0 diff --git a/modules/zencrm_entities/contact_details.page.inc b/modules/zencrm_entities/contact_details.page.inc new file mode 100644 index 0000000..4924125 --- /dev/null +++ b/modules/zencrm_entities/contact_details.page.inc @@ -0,0 +1,30 @@ +isPublished()) { + return AccessResult::allowedIfHasPermission($account, 'view unpublished contact details entities'); + } + return AccessResult::allowedIfHasPermission($account, 'view published contact details entities'); + + case 'update': + return AccessResult::allowedIfHasPermission($account, 'edit contact details entities'); + + case 'delete': + return AccessResult::allowedIfHasPermission($account, 'delete contact details entities'); + } + + // Unknown operation, no opinion. + return AccessResult::neutral(); + } + + /** + * {@inheritdoc} + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + return AccessResult::allowedIfHasPermission($account, 'add contact details entities'); + } + +} diff --git a/modules/zencrm_entities/src/ContactDetailsHtmlRouteProvider.php b/modules/zencrm_entities/src/ContactDetailsHtmlRouteProvider.php new file mode 100644 index 0000000..55e0d14 --- /dev/null +++ b/modules/zencrm_entities/src/ContactDetailsHtmlRouteProvider.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\ContactDetailsController::revisionOverview', + ]) + ->setRequirement('_permission', 'access contact details 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\ContactDetailsController::revisionShow', + '_title_callback' => '\Drupal\zencrm_entities\Controller\ContactDetailsController::revisionPageTitle', + ]) + ->setRequirement('_permission', 'access contact details 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\ContactDetailsRevisionRevertForm', + '_title' => 'Revert to earlier revision', + ]) + ->setRequirement('_permission', 'revert all contact details 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\ContactDetailsRevisionDeleteForm', + '_title' => 'Delete earlier revision', + ]) + ->setRequirement('_permission', 'delete all contact details 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\ContactDetailsRevisionRevertTranslationForm', + '_title' => 'Revert to earlier revision of a translation', + ]) + ->setRequirement('_permission', 'revert all contact details 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\ContactDetailsSettingsForm', + '_title' => "{$entity_type->getLabel()} settings", + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/modules/zencrm_entities/src/ContactDetailsListBuilder.php b/modules/zencrm_entities/src/ContactDetailsListBuilder.php new file mode 100644 index 0000000..0ede242 --- /dev/null +++ b/modules/zencrm_entities/src/ContactDetailsListBuilder.php @@ -0,0 +1,40 @@ +t('Contact Details ID'); + $header['name'] = $this->t('Name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + /* @var $entity \Drupal\zencrm_entities\Entity\ContactDetails */ + $row['id'] = $entity->id(); + $row['name'] = Link::createFromRoute( + $entity->label(), + 'entity.contact_details.edit_form', + ['contact_details' => $entity->id()] + ); + return $row + parent::buildRow($entity); + } + +} diff --git a/modules/zencrm_entities/src/ContactDetailsStorage.php b/modules/zencrm_entities/src/ContactDetailsStorage.php new file mode 100644 index 0000000..b2613f0 --- /dev/null +++ b/modules/zencrm_entities/src/ContactDetailsStorage.php @@ -0,0 +1,58 @@ +database->query( + 'SELECT vid FROM {contact_details_revision} WHERE id=:id ORDER BY vid', + [':id' => $entity->id()] + )->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function userRevisionIds(AccountInterface $account) { + return $this->database->query( + 'SELECT vid FROM {contact_details_field_revision} WHERE uid = :uid ORDER BY vid', + [':uid' => $account->id()] + )->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function countDefaultLanguageRevisions(ContactDetailsInterface $entity) { + return $this->database->query('SELECT COUNT(*) FROM {contact_details_field_revision} WHERE id = :id AND default_langcode = 1', [':id' => $entity->id()]) + ->fetchField(); + } + + /** + * {@inheritdoc} + */ + public function clearRevisionsLanguage(LanguageInterface $language) { + return $this->database->update('contact_details_revision') + ->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]) + ->condition('langcode', $language->getId()) + ->execute(); + } + +} diff --git a/modules/zencrm_entities/src/ContactDetailsStorageInterface.php b/modules/zencrm_entities/src/ContactDetailsStorageInterface.php new file mode 100644 index 0000000..01709f0 --- /dev/null +++ b/modules/zencrm_entities/src/ContactDetailsStorageInterface.php @@ -0,0 +1,61 @@ +entityManager()->getStorage('contact_details')->loadRevision($contact_details_revision); + $view_builder = $this->entityManager()->getViewBuilder('contact_details'); + + return $view_builder->view($contact_details); + } + + /** + * Page title callback for a Contact Details revision. + * + * @param int $contact_details_revision + * The Contact Details revision ID. + * + * @return string + * The page title. + */ + public function revisionPageTitle($contact_details_revision) { + $contact_details = $this->entityManager()->getStorage('contact_details')->loadRevision($contact_details_revision); + return $this->t('Revision of %title from %date', ['%title' => $contact_details->label(), '%date' => format_date($contact_details->getRevisionCreationTime())]); + } + + /** + * Generates an overview table of older revisions of a Contact Details . + * + * @param \Drupal\zencrm_entities\Entity\ContactDetailsInterface $contact_details + * A Contact Details object. + * + * @return array + * An array as expected by drupal_render(). + */ + public function revisionOverview(ContactDetailsInterface $contact_details) { + $account = $this->currentUser(); + $langcode = $contact_details->language()->getId(); + $langname = $contact_details->language()->getName(); + $languages = $contact_details->getTranslationLanguages(); + $has_translations = (count($languages) > 1); + $contact_details_storage = $this->entityManager()->getStorage('contact_details'); + + $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $contact_details->label()]) : $this->t('Revisions for %title', ['%title' => $contact_details->label()]); + $header = [$this->t('Revision'), $this->t('Operations')]; + + $revert_permission = (($account->hasPermission("revert all contact details revisions") || $account->hasPermission('administer contact details entities'))); + $delete_permission = (($account->hasPermission("delete all contact details revisions") || $account->hasPermission('administer contact details entities'))); + + $rows = []; + + $vids = $contact_details_storage->revisionIds($contact_details); + + $latest_revision = TRUE; + + foreach (array_reverse($vids) as $vid) { + /** @var \Drupal\zencrm_entities\ContactDetailsInterface $revision */ + $revision = $contact_details_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 != $contact_details->getRevisionId()) { + $link = $this->l($date, new Url('entity.contact_details.revision', ['contact_details' => $contact_details->id(), 'contact_details_revision' => $vid])); + } + else { + $link = $contact_details->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.contact_details.translation_revert', ['contact_details' => $contact_details->id(), 'contact_details_revision' => $vid, 'langcode' => $langcode]) : + Url::fromRoute('entity.contact_details.revision_revert', ['contact_details' => $contact_details->id(), 'contact_details_revision' => $vid]), + ]; + } + + if ($delete_permission) { + $links['delete'] = [ + 'title' => $this->t('Delete'), + 'url' => Url::fromRoute('entity.contact_details.revision_delete', ['contact_details' => $contact_details->id(), 'contact_details_revision' => $vid]), + ]; + } + + $row[] = [ + 'data' => [ + '#type' => 'operations', + '#links' => $links, + ], + ]; + } + + $rows[] = $row; + } + } + + $build['contact_details_revisions_table'] = [ + '#theme' => 'table', + '#rows' => $rows, + '#header' => $header, + ]; + + return $build; + } + +} diff --git a/modules/zencrm_entities/src/Entity/ContactDetails.php b/modules/zencrm_entities/src/Entity/ContactDetails.php new file mode 100644 index 0000000..caa42db --- /dev/null +++ b/modules/zencrm_entities/src/Entity/ContactDetails.php @@ -0,0 +1,304 @@ + \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 contact_details owner the + // revision author. + if (!$this->getRevisionUser()) { + $this->setRevisionUserId($this->getOwnerId()); + } + } + + /** + * {@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 Contact Details 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['person'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Person')) + ->setDescription(t('The person this profile is of.')) + ->setSetting('target_type', 'person') + ->setSetting('handler', 'views') + ->setSetting('handler_settings', [ + 'view' => [ + 'view_name' => 'persons', + 'display_name' => 'entity_reference_1', + 'arguments' => [] + ] + ]) + ->setDisplayOptions('form', [ + 'type' => 'entity_reference_autocomplete', + 'weight' => 5, + 'settings' => [ + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ], + ]) + ->setDisplayOptions('view', [ + 'label' => 'above', + 'weight' => 0, + ]); + + $fields['name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Name')) + ->setDescription(t('The name of the Contact Details entity.')) + ->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('Publishing status')) + ->setDescription(t('A boolean indicating whether the Contact Details is published.')) + ->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/ContactDetailsInterface.php b/modules/zencrm_entities/src/Entity/ContactDetailsInterface.php new file mode 100644 index 0000000..583ef18 --- /dev/null +++ b/modules/zencrm_entities/src/Entity/ContactDetailsInterface.php @@ -0,0 +1,116 @@ +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 Contact Details.', [ + '%label' => $entity->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label Contact Details.', [ + '%label' => $entity->label(), + ])); + } + $form_state->setRedirect('entity.contact_details.canonical', ['contact_details' => $entity->id()]); + } + +} diff --git a/modules/zencrm_entities/src/Form/ContactDetailsRevisionDeleteForm.php b/modules/zencrm_entities/src/Form/ContactDetailsRevisionDeleteForm.php new file mode 100644 index 0000000..4aa9dae --- /dev/null +++ b/modules/zencrm_entities/src/Form/ContactDetailsRevisionDeleteForm.php @@ -0,0 +1,123 @@ +ContactDetailsStorage = $entity_storage; + $this->connection = $connection; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + $entity_manager = $container->get('entity.manager'); + return new static( + $entity_manager->getStorage('contact_details'), + $container->get('database') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'contact_details_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.contact_details.version_history', ['contact_details' => $this->revision->id()]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $contact_details_revision = NULL) { + $this->revision = $this->ContactDetailsStorage->loadRevision($contact_details_revision); + $form = parent::buildForm($form, $form_state); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->ContactDetailsStorage->deleteRevision($this->revision->getRevisionId()); + + $this->logger('content')->notice('Contact Details: deleted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + drupal_set_message(t('Revision from %revision-date of Contact Details %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()])); + $form_state->setRedirect( + 'entity.contact_details.canonical', + ['contact_details' => $this->revision->id()] + ); + if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {contact_details_field_revision} WHERE id = :id', [':id' => $this->revision->id()])->fetchField() > 1) { + $form_state->setRedirect( + 'entity.contact_details.version_history', + ['contact_details' => $this->revision->id()] + ); + } + } + +} diff --git a/modules/zencrm_entities/src/Form/ContactDetailsRevisionRevertForm.php b/modules/zencrm_entities/src/Form/ContactDetailsRevisionRevertForm.php new file mode 100644 index 0000000..6063050 --- /dev/null +++ b/modules/zencrm_entities/src/Form/ContactDetailsRevisionRevertForm.php @@ -0,0 +1,149 @@ +ContactDetailsStorage = $entity_storage; + $this->dateFormatter = $date_formatter; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorage('contact_details'), + $container->get('date.formatter') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'contact_details_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.contact_details.version_history', ['contact_details' => $this->revision->id()]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return t('Revert'); + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return ''; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $contact_details_revision = NULL) { + $this->revision = $this->ContactDetailsStorage->loadRevision($contact_details_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('Contact Details: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + drupal_set_message(t('Contact Details %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.contact_details.version_history', + ['contact_details' => $this->revision->id()] + ); + } + + /** + * Prepares a revision to be reverted. + * + * @param \Drupal\zencrm_entities\Entity\ContactDetailsInterface $revision + * The revision to be reverted. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return \Drupal\zencrm_entities\Entity\ContactDetailsInterface + * The prepared revision ready to be stored. + */ + protected function prepareRevertedRevision(ContactDetailsInterface $revision, FormStateInterface $form_state) { + $revision->setNewRevision(); + $revision->isDefaultRevision(TRUE); + $revision->setRevisionCreationTime(REQUEST_TIME); + + return $revision; + } + +} diff --git a/modules/zencrm_entities/src/Form/ContactDetailsRevisionRevertTranslationForm.php b/modules/zencrm_entities/src/Form/ContactDetailsRevisionRevertTranslationForm.php new file mode 100644 index 0000000..2bcc6ff --- /dev/null +++ b/modules/zencrm_entities/src/Form/ContactDetailsRevisionRevertTranslationForm.php @@ -0,0 +1,115 @@ +languageManager = $language_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorage('contact_details'), + $container->get('date.formatter'), + $container->get('language_manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'contact_details_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, $contact_details_revision = NULL, $langcode = NULL) { + $this->langcode = $langcode; + $form = parent::buildForm($form, $form_state, $contact_details_revision); + + $form['revert_untranslated_fields'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Revert content shared among translations'), + '#default_value' => FALSE, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + protected function prepareRevertedRevision(ContactDetailsInterface $revision, FormStateInterface $form_state) { + $revert_untranslated_fields = $form_state->getValue('revert_untranslated_fields'); + + /** @var \Drupal\zencrm_entities\Entity\ContactDetailsInterface $default_revision */ + $latest_revision = $this->ContactDetailsStorage->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/ContactDetailsSettingsForm.php b/modules/zencrm_entities/src/Form/ContactDetailsSettingsForm.php new file mode 100644 index 0000000..315d7b5 --- /dev/null +++ b/modules/zencrm_entities/src/Form/ContactDetailsSettingsForm.php @@ -0,0 +1,53 @@ + + {% 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 142652b..a4527ce 100644 --- a/modules/zencrm_entities/zencrm_entities.links.action.yml +++ b/modules/zencrm_entities/zencrm_entities.links.action.yml @@ -14,3 +14,8 @@ entity.profile_type.add_form: appears_on: - entity.profile_type.collection +entity.contact_details.add_form: + route_name: entity.contact_details.add_form + title: 'Add Contact Details' + appears_on: + - entity.contact_details.collection diff --git a/modules/zencrm_entities/zencrm_entities.links.menu.yml b/modules/zencrm_entities/zencrm_entities.links.menu.yml index c3f4d63..87ab050 100644 --- a/modules/zencrm_entities/zencrm_entities.links.menu.yml +++ b/modules/zencrm_entities/zencrm_entities.links.menu.yml @@ -30,3 +30,17 @@ entity.profile_type.collection: parent: system.admin_structure weight: 99 + +# Contact Details menu items definition +entity.contact_details.collection: + title: 'Contact Details list' + route_name: entity.contact_details.collection + description: 'List Contact Details entities' + parent: system.admin_structure + weight: 100 + +contact_details.admin.structure.settings: + title: 'Contact Details settings' + description: 'Configure Contact Details entities' + route_name: contact_details.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 index 6ce894f..601e202 100644 --- a/modules/zencrm_entities/zencrm_entities.links.task.yml +++ b/modules/zencrm_entities/zencrm_entities.links.task.yml @@ -48,3 +48,30 @@ entity.profile.delete_form: title: Delete weight: 10 +# Contact Details routing definition +contact_details.settings_tab: + route_name: contact_details.settings + title: 'Settings' + base_route: contact_details.settings + +entity.contact_details.canonical: + route_name: entity.contact_details.canonical + base_route: entity.contact_details.canonical + title: 'View' + +entity.contact_details.edit_form: + route_name: entity.contact_details.edit_form + base_route: entity.contact_details.canonical + title: 'Edit' + +entity.contact_details.version_history: + route_name: entity.contact_details.version_history + base_route: entity.contact_details.canonical + title: 'Revisions' + +entity.contact_details.delete_form: + route_name: entity.contact_details.delete_form + base_route: entity.contact_details.canonical + title: Delete + weight: 10 + diff --git a/modules/zencrm_entities/zencrm_entities.permissions.yml b/modules/zencrm_entities/zencrm_entities.permissions.yml index 5e56d93..363d994 100644 --- a/modules/zencrm_entities/zencrm_entities.permissions.yml +++ b/modules/zencrm_entities/zencrm_entities.permissions.yml @@ -58,3 +58,33 @@ revert all profile revisions: delete all profile revisions: title: 'Delete all revisions' description: 'Role requires permission to view Profile revisions and delete rights for profile entities in question or administer profile entities.' +add contact details entities: + title: 'Create new Contact Details entities' + +administer contact details entities: + title: 'Administer Contact Details entities' + description: 'Allow to access the administration form to configure Contact Details entities.' + restrict access: true + +delete contact details entities: + title: 'Delete Contact Details entities' + +edit contact details entities: + title: 'Edit Contact Details entities' + +view published contact details entities: + title: 'View published Contact Details entities' + +view unpublished contact details entities: + title: 'View unpublished Contact Details entities' + +view all contact details revisions: + title: 'View all Contact Details revisions' + +revert all contact details revisions: + title: 'Revert all Contact Details revisions' + description: 'Role requires permission view Contact Details revisions and edit rights for contact details entities in question or administer contact details entities.' + +delete all contact details revisions: + title: 'Delete all revisions' + description: 'Role requires permission to view Contact Details revisions and delete rights for contact details entities in question or administer contact details entities.'