2018-04-06 09:14:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Drupal\zencrm_entities\Entity;
|
|
|
|
|
|
|
|
use Drupal\Core\Entity\EntityStorageInterface;
|
|
|
|
use Drupal\Core\Field\BaseFieldDefinition;
|
2018-04-11 09:42:45 +00:00
|
|
|
use Drupal\Core\Entity\ContentEntityBase;
|
2018-04-06 09:14:12 +00:00
|
|
|
use Drupal\Core\Entity\EntityChangedTrait;
|
|
|
|
use Drupal\Core\Entity\EntityTypeInterface;
|
|
|
|
use Drupal\user\UserInterface;
|
|
|
|
|
|
|
|
/**
|
2018-04-11 09:42:45 +00:00
|
|
|
* Defines the Contact details entity.
|
2018-04-06 09:14:12 +00:00
|
|
|
*
|
|
|
|
* @ingroup zencrm_entities
|
|
|
|
*
|
|
|
|
* @ContentEntityType(
|
|
|
|
* id = "contact_details",
|
2018-04-11 09:42:45 +00:00
|
|
|
* label = @Translation("Contact details"),
|
2018-04-06 09:14:12 +00:00
|
|
|
* handlers = {
|
|
|
|
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
|
|
|
|
* "list_builder" = "Drupal\zencrm_entities\ContactDetailsListBuilder",
|
|
|
|
* "views_data" = "Drupal\zencrm_entities\Entity\ContactDetailsViewsData",
|
|
|
|
*
|
|
|
|
* "form" = {
|
|
|
|
* "default" = "Drupal\zencrm_entities\Form\ContactDetailsForm",
|
|
|
|
* "add" = "Drupal\zencrm_entities\Form\ContactDetailsForm",
|
|
|
|
* "edit" = "Drupal\zencrm_entities\Form\ContactDetailsForm",
|
|
|
|
* "delete" = "Drupal\zencrm_entities\Form\ContactDetailsDeleteForm",
|
|
|
|
* },
|
|
|
|
* "access" = "Drupal\zencrm_entities\ContactDetailsAccessControlHandler",
|
|
|
|
* "route_provider" = {
|
|
|
|
* "html" = "Drupal\zencrm_entities\ContactDetailsHtmlRouteProvider",
|
|
|
|
* },
|
|
|
|
* },
|
|
|
|
* base_table = "contact_details",
|
|
|
|
* admin_permission = "administer contact details entities",
|
|
|
|
* entity_keys = {
|
|
|
|
* "id" = "id",
|
2018-04-11 09:44:47 +00:00
|
|
|
* "label" = "type",
|
2018-04-06 09:14:12 +00:00
|
|
|
* "uuid" = "uuid",
|
|
|
|
* "uid" = "user_id",
|
|
|
|
* "langcode" = "langcode",
|
|
|
|
* "status" = "status",
|
|
|
|
* },
|
|
|
|
* links = {
|
2018-04-11 12:35:08 +00:00
|
|
|
* "canonical" = "/zencrm/contact_details/{contact_details}",
|
|
|
|
* "add-form" = "/zencrm/contact_details/add",
|
|
|
|
* "edit-form" = "/zencrm/contact_details/{contact_details}/edit",
|
|
|
|
* "delete-form" = "/zencrm/contact_details/{contact_details}/delete",
|
|
|
|
* "collection" = "/zencrm/contact_details",
|
2018-04-06 09:14:12 +00:00
|
|
|
* },
|
|
|
|
* field_ui_base_route = "contact_details.settings"
|
|
|
|
* )
|
|
|
|
*/
|
2018-04-11 09:42:45 +00:00
|
|
|
class ContactDetails extends ContentEntityBase implements ContactDetailsInterface {
|
2018-04-06 09:14:12 +00:00
|
|
|
|
|
|
|
use EntityChangedTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
|
|
|
|
parent::preCreate($storage_controller, $values);
|
|
|
|
$values += [
|
|
|
|
'user_id' => \Drupal::currentUser()->id(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return $this->get('name')->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setName($name) {
|
|
|
|
$this->set('name', $name);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getCreatedTime() {
|
|
|
|
return $this->get('created')->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setCreatedTime($timestamp) {
|
|
|
|
$this->set('created', $timestamp);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getOwner() {
|
|
|
|
return $this->get('user_id')->entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getOwnerId() {
|
|
|
|
return $this->get('user_id')->target_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setOwnerId($uid) {
|
|
|
|
$this->set('user_id', $uid);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setOwner(UserInterface $account) {
|
|
|
|
$this->set('user_id', $account->id());
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function isPublished() {
|
|
|
|
return (bool) $this->getEntityKey('status');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setPublished($published) {
|
|
|
|
$this->set('status', $published ? TRUE : FALSE);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
|
|
|
$fields = parent::baseFieldDefinitions($entity_type);
|
|
|
|
|
|
|
|
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
|
|
|
|
->setLabel(t('Authored by'))
|
2018-04-11 09:44:47 +00:00
|
|
|
->setDescription(t('The user ID of author of the Contact Details entity.'))
|
2018-04-06 09:14:12 +00:00
|
|
|
->setSetting('target_type', 'user')
|
|
|
|
->setSetting('handler', 'default')
|
2018-04-11 09:44:47 +00:00
|
|
|
# ->setDisplayOptions('view', [
|
|
|
|
# 'label' => 'inline',
|
|
|
|
# 'type' => 'author',
|
|
|
|
# 'weight' => 100,
|
|
|
|
# ])
|
|
|
|
# ->setDisplayOptions('form', [
|
|
|
|
# 'type' => 'entity_reference_autocomplete',
|
|
|
|
# 'weight' => 100,
|
|
|
|
# 'settings' => [
|
|
|
|
# 'match_operator' => 'CONTAINS',
|
|
|
|
# 'size' => '60',
|
|
|
|
# 'autocomplete_type' => 'tags',
|
|
|
|
# 'placeholder' => '',
|
|
|
|
# ],
|
|
|
|
# ])
|
|
|
|
->setTranslatable(TRUE);
|
|
|
|
|
|
|
|
|
|
|
|
// Type field is used in entity reference fields etc
|
|
|
|
// so it is not exposed to user configuration.
|
|
|
|
$fields['type'] = BaseFieldDefinition::create('string')
|
|
|
|
->setLabel(t('Type'))
|
|
|
|
->setDescription(t('E.g. Home, Business, Temporary'))
|
|
|
|
->setDisplayOptions('form', [
|
2018-04-11 09:30:22 +00:00
|
|
|
'label' => 'hidden',
|
2018-04-11 09:44:47 +00:00
|
|
|
'type' => 'text',
|
2018-04-06 14:14:40 +00:00
|
|
|
'weight' => 0,
|
2018-04-06 11:05:05 +00:00
|
|
|
])
|
2018-04-11 09:44:47 +00:00
|
|
|
->setDisplayOptions('view', array(
|
|
|
|
'label' => 'hidden',
|
|
|
|
'type' => 'string',
|
|
|
|
'weight' => 0,
|
|
|
|
))
|
|
|
|
->setRequired(TRUE);
|
|
|
|
|
2018-04-06 14:14:40 +00:00
|
|
|
|
2018-04-11 09:44:47 +00:00
|
|
|
// Person field is always set from the context so no form or display required.
|
|
|
|
$fields['person'] = BaseFieldDefinition::create('entity_reference')
|
|
|
|
->setLabel(t('Person'))
|
2018-04-11 10:18:27 +00:00
|
|
|
->setDescription(t('The person this set of contact details is for.'))
|
2018-04-11 09:44:47 +00:00
|
|
|
->setSetting('target_type', 'person')
|
|
|
|
->setRequired(TRUE);
|
|
|
|
|
|
|
|
// Type field is used for mailings,
|
|
|
|
// so it is not exposed to user configuration.
|
|
|
|
$fields['email'] = BaseFieldDefinition::create('string')
|
|
|
|
->setLabel(t('Email Address'))
|
|
|
|
->setSettings(array(
|
|
|
|
'default_value' => '',
|
|
|
|
'max_length' => 30,
|
2018-04-06 14:14:40 +00:00
|
|
|
'text_processing' => 0,
|
2018-04-11 09:44:47 +00:00
|
|
|
))
|
|
|
|
->setDisplayOptions('view', array(
|
|
|
|
'label' => 'hidden',
|
|
|
|
'type' => 'string',
|
|
|
|
'weight' => 0,
|
|
|
|
))
|
|
|
|
->setDisplayOptions('form', array(
|
|
|
|
'type' => 'string_textfield',
|
|
|
|
'weight' => 0,
|
|
|
|
));
|
|
|
|
|
|
|
|
$fields['phone'] = BaseFieldDefinition::create('string')
|
|
|
|
->setLabel(t('Main Phone Number'))
|
|
|
|
->setDisplayConfigurable('view', TRUE)
|
|
|
|
->setDisplayConfigurable('form', TRUE)
|
|
|
|
->setSettings(array(
|
|
|
|
'default_value' => '',
|
|
|
|
'max_length' => 20,
|
|
|
|
'text_processing' => 0,
|
|
|
|
))
|
|
|
|
->setDisplayOptions('view', array(
|
2018-04-06 14:14:40 +00:00
|
|
|
'label' => 'above',
|
|
|
|
'type' => 'string',
|
2018-04-11 09:44:47 +00:00
|
|
|
'weight' => 2,
|
|
|
|
))
|
|
|
|
->setDisplayOptions('form', array(
|
2018-04-06 14:14:40 +00:00
|
|
|
'type' => 'string_textfield',
|
2018-04-11 09:44:47 +00:00
|
|
|
'weight' => 2,
|
|
|
|
));
|
|
|
|
|
|
|
|
$fields['phone2'] = BaseFieldDefinition::create('string')
|
|
|
|
->setLabel(t('Alternative Phone Number'))
|
|
|
|
->setSettings(array(
|
|
|
|
'default_value' => '',
|
|
|
|
'max_length' => 20,
|
|
|
|
'text_processing' => 0,
|
|
|
|
))
|
|
|
|
->setDisplayConfigurable('view', TRUE)
|
2018-04-11 09:30:22 +00:00
|
|
|
->setDisplayConfigurable('form', TRUE)
|
2018-04-11 09:44:47 +00:00
|
|
|
->setDisplayOptions('view', array(
|
|
|
|
'label' => 'above',
|
|
|
|
'type' => 'string',
|
|
|
|
'weight' => 3,
|
|
|
|
))
|
|
|
|
->setDisplayOptions('form', array(
|
|
|
|
'type' => 'string_textfield',
|
|
|
|
'weight' => 3,
|
|
|
|
));
|
|
|
|
|
|
|
|
$fields['postal_address'] = BaseFieldDefinition::create('string_long')
|
|
|
|
->setLabel(t('Postal Address'))
|
|
|
|
->setDescription(t('Full address, apart from post code.'))
|
2018-04-11 09:42:45 +00:00
|
|
|
->setDisplayConfigurable('view', TRUE)
|
2018-04-11 09:44:47 +00:00
|
|
|
->setDisplayConfigurable('form', TRUE)
|
|
|
|
->setSettings(array(
|
|
|
|
'default_value' => '',
|
|
|
|
'max_length' => 255,
|
|
|
|
'text_processing' => 0,
|
|
|
|
))
|
|
|
|
->setDisplayOptions('view', array(
|
|
|
|
'label' => 'above',
|
|
|
|
'type' => 'text',
|
|
|
|
'weight' => 5,
|
|
|
|
))
|
|
|
|
->setDisplayOptions('form', array(
|
|
|
|
'type' => 'string_textarea',
|
|
|
|
'weight' => 5,
|
|
|
|
));
|
|
|
|
|
|
|
|
$fields['post_code'] = BaseFieldDefinition::create('string')
|
|
|
|
->setLabel(t('Post Code'))
|
|
|
|
->setSettings(array(
|
|
|
|
'default_value' => '',
|
|
|
|
'max_length' => 10,
|
|
|
|
'text_processing' => 0,
|
|
|
|
))
|
|
|
|
->setDisplayOptions('view', array(
|
|
|
|
'label' => 'above',
|
|
|
|
'type' => 'string',
|
|
|
|
'weight' => 6,
|
|
|
|
))
|
|
|
|
->setDisplayOptions('form', array(
|
|
|
|
'type' => 'string_textfield',
|
|
|
|
'weight' => 6,
|
|
|
|
));
|
2018-04-06 14:14:40 +00:00
|
|
|
|
2018-04-06 09:14:12 +00:00
|
|
|
$fields['status'] = BaseFieldDefinition::create('boolean')
|
2018-04-11 09:44:47 +00:00
|
|
|
->setLabel(t('Enabled'))
|
|
|
|
->setDescription(t('If this is ticked then this set of contact details is active.'))
|
|
|
|
# ->setDisplayOptions('form', [
|
|
|
|
# 'type' => 'boolean_checkbox',
|
|
|
|
# 'weight' => -3,
|
|
|
|
# ])
|
|
|
|
->setDefaultValue(TRUE);
|
2018-04-06 09:14:12 +00:00
|
|
|
|
|
|
|
$fields['created'] = BaseFieldDefinition::create('created')
|
|
|
|
->setLabel(t('Created'))
|
|
|
|
->setDescription(t('The time that the entity was created.'));
|
|
|
|
|
|
|
|
$fields['changed'] = BaseFieldDefinition::create('changed')
|
|
|
|
->setLabel(t('Changed'))
|
|
|
|
->setDescription(t('The time that the entity was last edited.'));
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|