From 6ae7f856ca33cbaecdfb9bbda7f099c5fde837d3 Mon Sep 17 00:00:00 2001 From: Yan Loetzer Date: Fri, 20 Oct 2017 11:16:49 -0400 Subject: [PATCH 1/4] by yanniboi: Added ability to require contact has hats. --- config/schema/contact_tab.schema.yml | 6 +++++ contacts.links.action.yml | 6 +++++ contacts.module | 14 ------------ src/ContactsTabManager.php | 6 +++++ src/Entity/ContactTab.php | 26 ++++++++++++++++++++- src/Entity/ContactTabInterface.php | 18 +++++++++++++++ src/Form/ContactTabForm.php | 34 ++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 15 deletions(-) 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/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..313027b 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 = []; + $roles = user_roles(TRUE); + foreach ($roles as $id => $role) { + $options[$id] = $role->label(); + } + + $form['required_hats'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Require hats'), + '#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} */ From baa08ccef659fd9c10913c89a3b721e827fdf85a Mon Sep 17 00:00:00 2001 From: Yan Loetzer Date: Fri, 20 Oct 2017 11:16:49 -0400 Subject: [PATCH 2/4] by yanniboi: Added ability to require contact has hats. --- config/schema/contact_tab.schema.yml | 6 +++++ contacts.links.action.yml | 6 +++++ contacts.module | 14 ------------ src/ContactsTabManager.php | 6 +++++ src/Entity/ContactTab.php | 26 ++++++++++++++++++++- src/Entity/ContactTabInterface.php | 18 +++++++++++++++ src/Form/ContactTabForm.php | 34 ++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 15 deletions(-) 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/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..313027b 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 = []; + $roles = user_roles(TRUE); + foreach ($roles as $id => $role) { + $options[$id] = $role->label(); + } + + $form['required_hats'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Require hats'), + '#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} */ From 4019eb3237e15fde55f21aa9d1e858f0b7981017 Mon Sep 17 00:00:00 2001 From: Yan Loetzer Date: Thu, 23 Nov 2017 09:38:42 -0500 Subject: [PATCH 3/4] by yanniboi: Updated for advanced roles. --- modules/crm_tools/crm_tools.module | 16 ++++++++++++++++ src/Form/ContactTabForm.php | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) 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/Form/ContactTabForm.php b/src/Form/ContactTabForm.php index 313027b..85c6abf 100644 --- a/src/Form/ContactTabForm.php +++ b/src/Form/ContactTabForm.php @@ -67,9 +67,9 @@ public function form(array $form, FormStateInterface $form_state) { ]; $options = []; - $roles = user_roles(TRUE); - foreach ($roles as $id => $role) { - $options[$id] = $role->label(); + $hats = contacts_user_hats(); + foreach ($hats as $id => $hat) { + $options[$id] = $hat->label(); } $form['required_hats'] = [ From d2ea1b672a5cd7250bd7143378b4664da44004b3 Mon Sep 17 00:00:00 2001 From: Yan Loetzer Date: Thu, 23 Nov 2017 09:45:26 -0500 Subject: [PATCH 4/4] by yanniboi: Updated wording. --- src/Form/ContactTabForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Form/ContactTabForm.php b/src/Form/ContactTabForm.php index 85c6abf..e5139c6 100644 --- a/src/Form/ContactTabForm.php +++ b/src/Form/ContactTabForm.php @@ -74,7 +74,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['required_hats'] = [ '#type' => 'checkboxes', - '#title' => $this->t('Require hats'), + '#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()),