Commit 1f19ac6f authored by mbovan's avatar mbovan Committed by slashrsm

Issue #2694357 by mbovan, Berdir, Denchev: Clean up entity type definitions,...

Issue #2694357 by mbovan, Berdir, Denchev: Clean up entity type definitions, use more standard functionality
parent a094a291
......@@ -7,7 +7,7 @@ class crm_core_contact_handler_field_contact_revert extends views_handler_field
function init(&$view, $options) {
parent::init($view, $options);
$this->additional_fields['contact_id'] = 'contact_id';
$this->additional_fields['vid'] = 'vid';
$this->additional_fields['revision_id'] = 'revision_id';
}
function access() {
......@@ -47,12 +47,12 @@ class crm_core_contact_handler_field_contact_revert extends views_handler_field
$max_vid = db_query("SELECT max(vid) FROM {crm_core_contact_revision} WHERE contact_id = :contact_id",
array(':contact_id' => $values->{$this->aliases['contact_id']}))->fetchField();
if ($values->{$this->aliases['vid']} == $max_vid) {
if ($values->{$this->aliases['revision_id']} == $max_vid) {
return t('current');
}
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = 'crm-core/contact/' . $values->{$this->aliases['contact_id']} . '/revert/' . $values->{$this->aliases['vid']};
$this->options['alter']['path'] = 'crm-core/contact/' . $values->{$this->aliases['contact_id']} . '/revert/' . $values->{$this->aliases['revision_id']};
$text = !empty($this->options['text']) ? $this->options['text'] : t('revert');
return $text;
......
......@@ -9,7 +9,7 @@ class crm_core_contact_handler_field_contact_revision extends views_handler_fiel
if (!empty($this->options['link_to_revision'])) {
$this->additional_fields['contact_id'] = 'contact_id';
$this->additional_fields['vid'] = 'vid';
$this->additional_fields['revision_id'] = 'revision_id';
}
}
......@@ -45,7 +45,7 @@ class crm_core_contact_handler_field_contact_revision extends views_handler_fiel
function render_link($data, $values) {
if (!empty($this->options['link_to_revision']) && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = 'crm-core/contact/' . $values->{$this->aliases['contact_id']} . '/revert/' . $values->{$this->aliases['vid']};
$this->options['alter']['path'] = 'crm-core/contact/' . $values->{$this->aliases['contact_id']} . '/revert/' . $values->{$this->aliases['revision_id']};
}
return $data;
......
......@@ -7,7 +7,7 @@ class crm_core_contact_handler_field_contact_revision extends views_handler_fiel
function construct() {
parent::construct();
$this->additional_fields['vid'] = 'vid';
$this->additional_fields['revision_id'] = 'revision_id';
}
function option_definition() {
......@@ -35,7 +35,7 @@ class crm_core_contact_handler_field_contact_revision extends views_handler_fiel
function render($values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('view');
$vid = $values->{$this->aliases['vid']};
$vid = $values->{$this->aliases['revision_id']};
return l($text, 'crm-core/contact/' . $vid);
}
......
......@@ -146,12 +146,12 @@ function crm_core_contact_views_data() {
$data['crm_core_contact_revision']['table']['group'] = t('CRM Core Contact revision');
$data['crm_core_contact_revision']['table']['base'] = array(
'field' => 'vid',
'field' => 'revision_id',
'title' => t('CRM Core Contact revision'),
'help' => t('Contact revisions from CRM Core.'),
);
$data['crm_core_contact_revision']['vid'] = array(
$data['crm_core_contact_revision']['revision_id'] = array(
'title' => t('Contact revision ID'),
'help' => t('The unique internal identifier for a contact revision.'),
'field' => array(
......
......@@ -21,7 +21,6 @@ use Drupal\crm_core_contact\ContactInterface;
* id = "crm_core_contact",
* label = @Translation("CRM Core Contact"),
* bundle_label = @Translation("Contact type"),
* label_callback = "Drupal\crm_core_contact\Entity\Contact::labelCallback",
* handlers = {
* "access" = "Drupal\crm_core_contact\ContactAccessControlHandler",
* "form" = {
......@@ -33,13 +32,11 @@ use Drupal\crm_core_contact\ContactInterface;
* },
* base_table = "crm_core_contact",
* revision_table = "crm_core_contact_revision",
* fieldable = TRUE,
* entity_keys = {
* "id" = "contact_id",
* "revision" = "vid",
* "revision" = "revision_id",
* "bundle" = "type",
* "uuid" = "uuid",
* "user" = "uid",
* },
* bundle_entity_type = "crm_core_contact_type",
* permission_granularity = "bundle",
......@@ -50,7 +47,7 @@ use Drupal\crm_core_contact\ContactInterface;
* links = {
* "canonical" = "/crm-core/contact/{crm_core_contact}",
* "collection" = "/crm-core/contact",
* "edit_form" = "/crm-core/contact/{crm_core_contact}/edit",
* "edit-form" = "/crm-core/contact/{crm_core_contact}/edit",
* "delete-form" = "/crm-core/contact/{crm_core_contact}/delete"
* }
* )
......@@ -65,30 +62,7 @@ class Contact extends ContentEntityBase implements ContactInterface {
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = array();
$fields['contact_id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Contact ID'))
->setDescription(t('The contact ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The contact UUID.'))
->setReadOnly(TRUE);
$fields['vid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The contact revision ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The contact type.'))
->setSetting('target_type', 'crm_core_contact_type')
->setReadOnly(TRUE);
$fields = parent::baseFieldDefinitions($entity_type);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
......@@ -134,12 +108,12 @@ class Contact extends ContentEntityBase implements ContactInterface {
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('form', array(
'type' => 'text_textarea',
'weight' => 25,
'settings' => array(
'rows' => 4,
),
));
'type' => 'text_textarea',
'weight' => 25,
'settings' => array(
'rows' => 4,
),
));
return $fields;
}
......@@ -214,15 +188,14 @@ class Contact extends ContentEntityBase implements ContactInterface {
$crm_core_activity_storage = \Drupal::entityTypeManager()->getStorage('crm_core_activity');
$activities = $crm_core_activity_storage->loadMultiple($activities_to_remove);
$ids = array_keys($entities);
\Drupal::logger('crm_core_activity')->info('Deleted !count activities due to deleting contact id=%contact_id.', [
'!count' => count($activities_to_remove),
\Drupal::logger('crm_core_activity')->info('Deleted @count activities due to deleting contact id=%contact_id.', [
'@count' => count($activities_to_remove),
'%contact_id' => reset($ids),
]);
$crm_core_activity_storage->delete($activities);
}
}
/**
* Gets the primary address.
*
......@@ -272,38 +245,17 @@ class Contact extends ContentEntityBase implements ContactInterface {
return $this->get($name);
}
/**
* Returns the label of the contact.
*
* @param \Drupal\crm_core_contact\Entity\Contact $entity
* The Contact entity.
*
* @return string
* Contact label.
*/
public static function labelCallback(Contact $entity) {
// @todo Replace with the value of the contact_name field, when name module will be available.
$label = $entity->get('name')->value;
\Drupal::moduleHandler()->alter('crm_core_contact_label', $label, $entity);
return $label;
}
/**
* {@inheritdoc}
*/
public function getChangedTime() {
return $this->changed;
}
/**
* {@inheritdoc}
*/
public function label() {
$label = parent::label();
// @todo Replace with the value of the contact_name field, when name module will be available.
$label = $this->get('name')->value;
if (empty($label)) {
$label = t('Nameless #@id', ['@id' => $this->id()]);
}
\Drupal::moduleHandler()->alter('crm_core_contact_label', $label, $entity);
return $label;
}
......
......@@ -23,7 +23,7 @@ use Drupal\crm_core_contact\ContactTypeInterface;
* "access" = "Drupal\crm_core_contact\ContactTypeAccessControlHandler",
* "form" = {
* "default" = "Drupal\crm_core_contact\Form\ContactTypeForm",
* "delete" = "Drupal\crm_core_contact\Form\ContactTypeDeleteForm",
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
* "toggle" = "Drupal\crm_core_contact\Form\ContactTypeToggleForm",
* },
* "list_builder" = "Drupal\crm_core_contact\ContactTypeListBuilder",
......@@ -135,7 +135,7 @@ class ContactType extends ConfigEntityBundleBase implements ContactTypeInterface
if (!empty($results)) {
$contacts = Contact::loadMultiple($results);
\Drupal::entityTypeManager()->getStorage('crm_core_contact')->delete($contacts);
watchdog('crm_core_contact', 'Delete !count contacts due to deletion of contact type.', array('!count' => count($results)), WATCHDOG_INFO);
\Drupal::logger('crm_core_contact')->info('Delete @count contacts due to deletion of contact type.', ['@count' => count($results)]);
}
}
......
<?php
/**
* @file
* Contains \Drupal\crm_core_contact\Form\ContactTypeDeleteForm.
*/
namespace Drupal\crm_core_contact\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContactTypeDeleteForm extends EntityConfirmFormBase {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a new NodeTypeDeleteConfirm object.
*
* @param \Drupal\Core\Database\Connection $database
* The database connection.
*/
public function __construct(Connection $database) {
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('database')
);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the contact type %type?', array('%type' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.crm_core_contact_type.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$num_nodes = $this->database->query("SELECT COUNT(*) FROM {crm_core_contact} WHERE type = :type", array(':type' => $this->entity->id()))->fetchField();
if ($num_nodes) {
$caption = \Drupal::translation()->formatPlural(
$num_nodes,
'%type is used by one contact on your site. You can not remove this contact type until you have removed all of the %type contacts.',
'%type is used by @count contacts on your site. You may not remove %type until you have removed all of the %type contacts.',
array('%type' => $this->entity->label()));
$form['#title'] = $this->getQuestion();
$form['description'] = array('#markup' => '<p>' . $caption . '</p>');
return $form;
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$t_args = array('%name' => $this->entity->label());
drupal_set_message($this->t('The contact type %name has been deleted.', $t_args));
\Drupal::logger('node')->notice('Deleted contact type %name.', $t_args);
$form_state->setRedirect('entity.crm_core_contact_type.collection');
}
}
......@@ -29,11 +29,11 @@ function crm_core_contact_ui_revision_form($contact) {
/**
* Form for reverting a contact to a previous version.
*/
function crm_core_contact_ui_revert_form($form, &$form_state, $contact, $vid) {
function crm_core_contact_ui_revert_form($form, &$form_state, $contact, $revision_id) {
$form['vid'] = array(
$form['revision_id'] = array(
'#type' => 'value',
'#value' => $vid,
'#value' => $revision_id,
);
$form_state['crm_core_ui']['contact'] = $contact;
......
......@@ -237,6 +237,8 @@ class ContactUiTest extends WebTestBase {
// Then I should not see a delete link.
$this->assertNoContactTypeLink('individual/delete', 'No delete link for individual.');
$this->drupalGet('admin/structure/crm-core/contact-types/individual/delete');
$this->assertResponse(403);
// When I edit the organization type.
$this->drupalGet('admin/structure/crm-core/contact-types/organization');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment