Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/schema/contact_tab.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ contacts.contact_tab.*:
sequence:
type: contacts.relationship.[%name]
label: 'Relationship'
hats:
type: sequence
label: 'Hats'
sequence:
type: string
label: 'Hat'
blocks:
type: sequence
label: 'Blocks'
Expand Down
6 changes: 6 additions & 0 deletions contacts.links.action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ contacts.contact_add_org:
- 'use-ajax'
data-dialog-type: 'modal'
data-ajax-progress: 'fullscreen'

contact_tab_add:
route_name: entity.contact_tab.add_form
title: 'Add contact tab'
appears_on:
- entity.contact_tab.collection
14 changes: 0 additions & 14 deletions contacts.module
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,6 @@ function template_preprocess_contacts_dash_tabs(array &$variables) {
}
}

/**
* Prepares variables for contact tab content.
*
* Default template: contact-tab-content.html.twig.
*
* @param array $variables
* An associative array containing:
* - attributes: HTML markup attributes for the content wrapper.
* - region_attributes: HTML markup attributes for the content wrapper.
*/
function template_preprocess_contact_tab_content(array &$variables) {
$variables['region_attributes'] = new Attribute($variables['region_attributes']);
}

/**
* Prepares variables for contact dashboard summary block.
*
Expand Down
16 changes: 16 additions & 0 deletions modules/crm_tools/crm_tools.module
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ function crm_tools_user_presave(UserInterface $account) {
}
}

/**
* Retrieves the names of roles matching specified conditions.
*
* @return \Drupal\user\RoleInterface[]
* An associative array with the role id as the key and the role name as
* value.
*/
function contacts_user_hats() {
return array_filter(array_map(function ($item) {
/* @var \Drupal\user\RoleInterface $item */
if ($item->getThirdPartySetting('crm_tools', 'crm_tools_is_hat')) {
return $item;
}
}, user_roles(TRUE)));
}

/**
* Gets color palette options for the crm hats.
*
Expand Down
6 changes: 6 additions & 0 deletions src/ContactsTabManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ public function getTabs(UserInterface $contact) {
if (!$this->verifyTab($tab, $contact)) {
unset($tabs[$id]);
}

if (!empty($tab->getHats())) {
if (empty(array_intersect($contact->getRoles(), $tab->getHats()))) {
unset($tabs[$id]);
}
}
}

// Sort our tabs by weight.
Expand Down
26 changes: 25 additions & 1 deletion src/Entity/ContactTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* handlers = {
* "list_builder" = "Drupal\contacts\ContactTabListBuilder",
* "form" = {
* "add" = "Drupal\contacts\Form\ContactTabForm",
* "edit" = "Drupal\contacts\Form\ContactTabForm",
* "delete" = "Drupal\contacts\Form\ContactTabDeleteForm"
* },
Expand All @@ -29,6 +30,7 @@
* },
* links = {
* "canonical" = "/admin/structure/contact-tabs/{contact_tab}",
* "add-form" = "/admin/structure/contact-tabs/add",
* "edit-form" = "/admin/structure/contact-tabs/{contact_tab}/edit",
* "delete-form" = "/admin/structure/contact-tabs/{contact_tab}/delete",
* "collection" = "/admin/structure/contact-tabs"
Expand Down Expand Up @@ -78,7 +80,14 @@ class ContactTab extends ConfigEntityBase implements ContactTabInterface {
protected $relationships = [];

/**
* The blocks configuration.
* The hats required for the tab.
*
* @var string[]
*/
protected $hats = [];

/**
* The block configuration.
*
* An array including:
* - id: The block plugin id.
Expand Down Expand Up @@ -125,6 +134,21 @@ public function setRelationships(array $relationships) {
return $this;
}

/**
* {@inheritdoc}
*/
public function getHats() {
return $this->hats;
}

/**
* {@inheritdoc}
*/
public function setHats(array $hats) {
$this->hats = $hats;
return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Entity/ContactTabInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ public function getPath();
*/
public function setPath($path);

/**
* Get the hats required by the tab.
*
* @return array
* An array of hat ids.
*/
public function getHats();

/**
* Set the hats required by the tab.
*
* @param array $hats
* An array of hat ids.
*
* @return $this
*/
public function setHats(array $hats);

/**
* Get the relationship definitions.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Form/ContactTabForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
*/
class ContactTabForm extends EntityForm {

/**
* The contact tab entity being used by this form.
*
* @var \Drupal\contacts\Entity\ContactTab
*/
protected $entity;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -59,9 +66,36 @@ public function form(array $form, FormStateInterface $form_state) {
'standalone' => TRUE,
];

$options = [];
$hats = contacts_user_hats();
foreach ($hats as $id => $hat) {
$options[$id] = $hat->label();
}

$form['required_hats'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Require contact types'),
'#description' => $this->t('Require that a user has at least one of these hats to show this tab.'),
'#options' => $options,
'#default_value' => array_keys($this->entity->getHats()),
];

return $form;
}

/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state) {
/* @var \Drupal\contacts\Entity\ContactTab $entity */
$entity = parent::buildEntity($form, $form_state);

$required_hats = $form_state->getValue('required_hats');
$entity->setHats(array_filter($required_hats));

return $entity;
}

/**
* {@inheritdoc}
*/
Expand Down