removed all person stuff and rand update entiees

This commit is contained in:
naomi 2018-04-11 10:44:29 +02:00
parent c47ce313b3
commit add15f2c6d
15 changed files with 117 additions and 973 deletions

View File

@ -1,163 +0,0 @@
<?php
namespace Drupal\zencrm_entities\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Url;
use Drupal\zencrm_entities\Entity\PersonInterface;
/**
* Class PersonController.
*
* Returns responses for Person routes.
*/
class PersonController extends ControllerBase implements ContainerInjectionInterface {
/**
* Displays a Person revision.
*
* @param int $person_revision
* The Person revision ID.
*
* @return array
* An array suitable for drupal_render().
*/
public function revisionShow($person_revision) {
$person = $this->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 %}<p class="revision-log">{{ message }}</p>{% 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' => '<em>',
'#markup' => $this->t('Current revision'),
'#suffix' => '</em>',
],
];
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;
}
}

View File

@ -4,8 +4,7 @@ namespace Drupal\zencrm_entities\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
@ -19,11 +18,9 @@ use Drupal\user\UserInterface;
* id = "person",
* label = @Translation("Person"),
* handlers = {
* "storage" = "Drupal\zencrm_entities\PersonStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\zencrm_entities\PersonListBuilder",
* "views_data" = "Drupal\zencrm_entities\Entity\PersonViewsData",
* "translation" = "Drupal\zencrm_entities\PersonTranslationHandler",
*
* "form" = {
* "default" = "Drupal\zencrm_entities\Form\PersonForm",
@ -37,36 +34,26 @@ use Drupal\user\UserInterface;
* },
* },
* base_table = "person",
* data_table = "person_field_data",
* revision_table = "person_revision",
* revision_data_table = "person_field_revision",
* translatable = TRUE,
* admin_permission = "administer person entities",
* entity_keys = {
* "id" = "id",
* "revision" = "vid",
* "label" = "full_name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* "label" = "full_name",
* },
* links = {
* "canonical" = "/zencrm/person/{person}",
* "add-form" = "/zencrm/person/add",
* "edit-form" = "/zencrm/person/{person}/edit",
* "delete-form" = "/zencrm/person/{person}/delete",
* "version-history" = "/zencrm/person/{person}/revisions",
* "revision" = "/zencrm/person/{person}/revisions/{person_revision}/view",
* "revision_revert" = "/zencrm/person/{person}/revisions/{person_revision}/revert",
* "revision_delete" = "/zencrm/person/{person}/revisions/{person_revision}/delete",
* "translation_revert" = "/zencrm/person/{person}/revisions/{person_revision}/revert/{langcode}",
* "collection" = "/zencrm/person",
* "canonical" = "/admin/structure/person/{person}",
* "add-form" = "/admin/structure/person/add",
* "edit-form" = "/admin/structure/person/{person}/edit",
* "delete-form" = "/admin/structure/person/{person}/delete",
* "collection" = "/admin/structure/person",
* },
* field_ui_base_route = "person.settings"
* )
*/
class Person extends RevisionableContentEntityBase implements PersonInterface {
class Person extends ContentEntityBase implements PersonInterface {
use EntityChangedTrait;
@ -83,42 +70,18 @@ class Person extends RevisionableContentEntityBase implements PersonInterface {
/**
* {@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;
public function getName() {
return $this->get('name')->value;
}
/**
* {@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());
}
public function setName($name) {
$this->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
@ -191,34 +154,28 @@ class Person extends RevisionableContentEntityBase implements PersonInterface {
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
# ->setDisplayOptions('form', [
# 'type' => 'entity_reference_autocomplete',
# 'weight' => 5,
# 'settings' => [
# 'match_operator' => 'CONTAINS',
# 'size' => '60',
# 'autocomplete_type' => 'tags',
# 'placeholder' => '',
# ],
# ])
# ->setDisplayOptions('view', [
# 'label' => 'inline',
# 'type' => 'author',
# 'weight' => 100,
# ])
# ->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' => '',
# ],
# ])
->setTranslatable(TRUE);
// This field is computed in a presave hook.
$fields['full_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Full Name'))
->setDescription(t('The full name of the person.'))
->setRevisionable(TRUE);
// This field is computed in a presave hook.
$fields['first_and_last_name'] = BaseFieldDefinition::create('string')
->setLabel(t('First and Last Name'))
->setDescription(t('The first and last name of the person. Used for searching and autocomplete'))
->setRevisionable(TRUE);
->setDescription(t('The full name of the person.'));
$fields['first_name'] = BaseFieldDefinition::create('string')
->setLabel(t('First Name'))
@ -238,7 +195,6 @@ class Person extends RevisionableContentEntityBase implements PersonInterface {
$fields['middle_names'] = BaseFieldDefinition::create('string')
->setLabel(t('Middle Names'))
->setDescription(t('Middle Names.'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
@ -249,10 +205,10 @@ class Person extends RevisionableContentEntityBase implements PersonInterface {
])
->setDisplayConfigurable('form', TRUE);
$fields['last_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Last Name'))
->setDescription(t('Last Name.'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
@ -265,13 +221,12 @@ class Person extends RevisionableContentEntityBase implements PersonInterface {
->setRequired(TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Enabled'))
->setDescription(t('If this is ticked, the record is active'))
->setRevisionable(TRUE)
# ->setDisplayOptions('form', [
# 'type' => 'boolean_checkbox',
# 'weight' => -3,
# ])
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Person is published.'))
# ->setDisplayOptions('form', [
# 'type' => 'boolean_checkbox',
# 'weight' => -3,
# ])
->setDefaultValue(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
@ -282,13 +237,6 @@ class Person extends RevisionableContentEntityBase implements PersonInterface {
->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;
}

View File

@ -3,7 +3,6 @@
namespace Drupal\zencrm_entities\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\user\EntityOwnerInterface;
@ -12,10 +11,29 @@ use Drupal\user\EntityOwnerInterface;
*
* @ingroup zencrm_entities
*/
interface PersonInterface extends ContentEntityInterface, RevisionLogInterface, EntityChangedInterface, EntityOwnerInterface {
interface PersonInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface {
// Add get/set methods for your configuration properties here.
/**
* Gets the Person name.
*
* @return string
* Name of the Person.
*/
public function getName();
/**
* Sets the Person name.
*
* @param string $name
* The Person name.
*
* @return \Drupal\zencrm_entities\Entity\PersonInterface
* The called Person entity.
*/
public function setName($name);
/**
* Gets the Person creation timestamp.
*
@ -56,42 +74,4 @@ interface PersonInterface extends ContentEntityInterface, RevisionLogInterface,
*/
public function setPublished($published);
/**
* Gets the Person revision creation timestamp.
*
* @return int
* The UNIX timestamp of when this revision was created.
*/
public function getRevisionCreationTime();
/**
* Sets the Person revision creation timestamp.
*
* @param int $timestamp
* The UNIX timestamp of when this revision was created.
*
* @return \Drupal\zencrm_entities\Entity\PersonInterface
* The called Person entity.
*/
public function setRevisionCreationTime($timestamp);
/**
* Gets the Person revision author.
*
* @return \Drupal\user\UserInterface
* The user entity for the revision author.
*/
public function getRevisionUser();
/**
* Sets the Person revision author.
*
* @param int $uid
* The user ID of the revision author.
*
* @return \Drupal\zencrm_entities\Entity\PersonInterface
* The called Person entity.
*/
public function setRevisionUserId($uid);
}

View File

@ -19,15 +19,6 @@ class PersonForm extends ContentEntityForm {
/* @var $entity \Drupal\zencrm_entities\Entity\Person */
$form = parent::buildForm($form, $form_state);
if (!$this->entity->isNew()) {
$form['new_revision'] = [
'#type' => 'checkbox',
'#title' => $this->t('Create new revision'),
'#default_value' => FALSE,
'#weight' => 10,
];
}
$entity = $this->entity;
return $form;
@ -39,18 +30,6 @@ class PersonForm extends ContentEntityForm {
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) {

View File

@ -1,123 +0,0 @@
<?php
namespace Drupal\zencrm_entities\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for deleting a Person revision.
*
* @ingroup zencrm_entities
*/
class PersonRevisionDeleteForm extends ConfirmFormBase {
/**
* The Person revision.
*
* @var \Drupal\zencrm_entities\Entity\PersonInterface
*/
protected $revision;
/**
* The Person storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $PersonStorage;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Constructs a new PersonRevisionDeleteForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The entity storage.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(EntityStorageInterface $entity_storage, Connection $connection) {
$this->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()]
);
}
}
}

View File

@ -1,149 +0,0 @@
<?php
namespace Drupal\zencrm_entities\Form;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\zencrm_entities\Entity\PersonInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for reverting a Person revision.
*
* @ingroup zencrm_entities
*/
class PersonRevisionRevertForm extends ConfirmFormBase {
/**
* The Person revision.
*
* @var \Drupal\zencrm_entities\Entity\PersonInterface
*/
protected $revision;
/**
* The Person storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $PersonStorage;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a new PersonRevisionRevertForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The Person storage.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
$this->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;
}
}

View File

@ -1,115 +0,0 @@
<?php
namespace Drupal\zencrm_entities\Form;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\zencrm_entities\Entity\PersonInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for reverting a Person revision for a single translation.
*
* @ingroup zencrm_entities
*/
class PersonRevisionRevertTranslationForm extends PersonRevisionRevertForm {
/**
* The language to be reverted.
*
* @var string
*/
protected $langcode;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new PersonRevisionRevertTranslationForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The Person storage.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager) {
parent::__construct($entity_storage, $date_formatter);
$this->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;
}
}

View File

@ -22,26 +22,6 @@ class PersonHtmlRouteProvider extends AdminHtmlRouteProvider {
$entity_type_id = $entity_type->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);
}
@ -49,126 +29,6 @@ class PersonHtmlRouteProvider extends AdminHtmlRouteProvider {
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.
*

View File

@ -1,58 +0,0 @@
<?php
namespace Drupal\zencrm_entities;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\zencrm_entities\Entity\PersonInterface;
/**
* Defines the storage handler class for Person entities.
*
* This extends the base storage class, adding required special handling for
* Person entities.
*
* @ingroup zencrm_entities
*/
class PersonStorage extends SqlContentEntityStorage implements PersonStorageInterface {
/**
* {@inheritdoc}
*/
public function revisionIds(PersonInterface $entity) {
return $this->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();
}
}

View File

@ -1,61 +0,0 @@
<?php
namespace Drupal\zencrm_entities;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\zencrm_entities\Entity\PersonInterface;
/**
* Defines the storage handler class for Person entities.
*
* This extends the base storage class, adding required special handling for
* Person entities.
*
* @ingroup zencrm_entities
*/
interface PersonStorageInterface extends ContentEntityStorageInterface {
/**
* Gets a list of Person revision IDs for a specific Person.
*
* @param \Drupal\zencrm_entities\Entity\PersonInterface $entity
* The Person entity.
*
* @return int[]
* Person revision IDs (in ascending order).
*/
public function revisionIds(PersonInterface $entity);
/**
* Gets a list of revision IDs having a given user as Person author.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user entity.
*
* @return int[]
* Person revision IDs (in ascending order).
*/
public function userRevisionIds(AccountInterface $account);
/**
* Counts the number of revisions in the default language.
*
* @param \Drupal\zencrm_entities\Entity\PersonInterface $entity
* The Person entity.
*
* @return int
* The number of revisions in the default language.
*/
public function countDefaultLanguageRevisions(PersonInterface $entity);
/**
* Unsets the language for all Person with the given language.
*
* @param \Drupal\Core\Language\LanguageInterface $language
* The language object.
*/
public function clearRevisionsLanguage(LanguageInterface $language);
}

View File

@ -1,14 +0,0 @@
<?php
namespace Drupal\zencrm_entities;
use Drupal\content_translation\ContentTranslationHandler;
/**
* Defines the translation handler for person.
*/
class PersonTranslationHandler extends ContentTranslationHandler {
// Override here the needed methods from ContentTranslationHandler.
}

View File

@ -19,3 +19,8 @@ entity.contact_details.add_form:
title: 'Add Contact Details'
appears_on:
- entity.contact_details.collection
entity.person.add_form:
route_name: entity.person.add_form
title: 'Add Person'
appears_on:
- entity.person.collection

View File

@ -44,3 +44,17 @@ contact_details.admin.structure.settings:
description: 'Configure Contact Details entities'
route_name: contact_details.settings
parent: system.admin_structure
# 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

View File

@ -75,3 +75,25 @@ entity.contact_details.delete_form:
title: Delete
weight: 10
# 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.delete_form:
route_name: entity.person.delete_form
base_route: entity.person.canonical
title: Delete
weight: 10

View File

@ -88,3 +88,22 @@ revert all contact details revisions:
delete all contact details revisions:
title: 'Delete all revisions'
description: 'Role requires permission to <em>view Contact Details revisions</em> and <em>delete rights</em> for contact details entities in question or <em>administer contact details entities</em>.'
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'