diff --git a/config/schema/contact_tab.schema.yml b/config/schema/contact_tab.schema.yml index fb8f6ea..93eb9f9 100644 --- a/config/schema/contact_tab.schema.yml +++ b/config/schema/contact_tab.schema.yml @@ -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' diff --git a/contacts.links.action.yml b/contacts.links.action.yml index d31f469..1849971 100644 --- a/contacts.links.action.yml +++ b/contacts.links.action.yml @@ -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 diff --git a/contacts.module b/contacts.module index c94f834..560c188 100644 --- a/contacts.module +++ b/contacts.module @@ -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. * diff --git a/modules/crm_tools/crm_tools.module b/modules/crm_tools/crm_tools.module index 864a74f..bebd853 100644 --- a/modules/crm_tools/crm_tools.module +++ b/modules/crm_tools/crm_tools.module @@ -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. * diff --git a/src/ContactsTabManager.php b/src/ContactsTabManager.php index ff99c67..ff31e1d 100644 --- a/src/ContactsTabManager.php +++ b/src/ContactsTabManager.php @@ -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. diff --git a/src/Entity/ContactTab.php b/src/Entity/ContactTab.php index 59c1f43..396dd24 100644 --- a/src/Entity/ContactTab.php +++ b/src/Entity/ContactTab.php @@ -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" * }, @@ -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" @@ -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. @@ -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} */ diff --git a/src/Entity/ContactTabInterface.php b/src/Entity/ContactTabInterface.php index 09ecdde..3b882bd 100644 --- a/src/Entity/ContactTabInterface.php +++ b/src/Entity/ContactTabInterface.php @@ -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. * diff --git a/src/Form/ContactTabForm.php b/src/Form/ContactTabForm.php index cf13b2a..e5139c6 100644 --- a/src/Form/ContactTabForm.php +++ b/src/Form/ContactTabForm.php @@ -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} */ @@ -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} */