summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrobin.ingelbrecht2017-06-01 15:22:36 +0200
committerrobin.ingelbrecht2017-06-01 15:22:36 +0200
commit61ba1c25b6bdaf2a5ab5ac4e8759e03312a174db (patch)
treee677b8e9706eb7381593b63f260365fa8797d312
parent985922afc95bd09a379b6ae7b12033f0ca62c70a (diff)
Init commit D8 version
-rw-r--r--README.md (renamed from README.txt)0
-rw-r--r--domain_restriction_formatter.info7
-rw-r--r--domain_restriction_formatter.info.yml5
-rw-r--r--domain_restriction_formatter.module127
4 files changed, 63 insertions, 76 deletions
diff --git a/README.txt b/README.md
index 0fcfb2e..0fcfb2e 100644
--- a/README.txt
+++ b/README.md
diff --git a/domain_restriction_formatter.info b/domain_restriction_formatter.info
deleted file mode 100644
index 6f77c0a..0000000
--- a/domain_restriction_formatter.info
+++ /dev/null
@@ -1,7 +0,0 @@
-name = Domain restriction formatter
-description = Exposes a formatter to show or hide fields on certain domains.
-package = Domain Access
-core = 7.x
-
-dependencies[] = domain
-dependencies[] = field_formatter_settings \ No newline at end of file
diff --git a/domain_restriction_formatter.info.yml b/domain_restriction_formatter.info.yml
new file mode 100644
index 0000000..dd57ebd
--- /dev/null
+++ b/domain_restriction_formatter.info.yml
@@ -0,0 +1,5 @@
+name: Domain Restriction Formatter
+type: module
+description: 'Exposes an extra option on formatters to show or hide fields on certain domains.'
+package: Domain
+core: 8.x
diff --git a/domain_restriction_formatter.module b/domain_restriction_formatter.module
index dd347ed..b65237c 100644
--- a/domain_restriction_formatter.module
+++ b/domain_restriction_formatter.module
@@ -1,96 +1,85 @@
<?php
-/**
- * Holds hooks and general functionality.
- */
+
+use Drupal\Core\Routing\RouteMatchInterface;
+use Drupal\Core\Field\FormatterInterface;
+use Drupal\Core\Field\FieldDefinitionInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\domain\Entity\Domain;
+use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\Display\EntityDisplayInterface;
/**
* Implements hook_help().
*/
-function domain_restriction_formatter_help($path, $arg) {
- switch ($path) {
- case 'admin/help#domain_restriction_formatter':
- $output = file_get_contents(drupal_get_path('module', 'domain_restriction_formatter') . '/README.txt');
- return module_exists('markdown') ? module_invoke('markdown', 'filter', 'process', 0, -1, $output) : '<pre>' . $output . '</pre>';
+function domain_restriction_formatter_help($route_name, RouteMatchInterface $route_match) {
+ switch ($route_name) {
+ case 'help.page.domain_restriction_formatter':
+ $output = file_get_contents(drupal_get_path('module', 'domain_restriction_formatter') . '/README.md');
+ return '<pre>' . $output . '</pre>';
+ break;
}
}
/**
- * Implements hook_field_formatter_info_alter().
+ * Implements hook_field_formatter_third_party_settings_form().
*/
-function domain_restriction_formatter_field_formatter_info_alter(&$info) {
- foreach ($info as &$formatter) {
- $formatter['settings']['domain_restriction_formatter'] = '';
+function domain_restriction_formatter_field_formatter_third_party_settings_form(FormatterInterface $plugin, FieldDefinitionInterface $field_definition, $view_mode, $form, FormStateInterface $form_state) {
+ $options = [];
+
+ $domains = \Drupal\domain\Entity\Domain::loadMultiple();
+ foreach ($domains as $domain) {
+ /** @var \Drupal\domain\DomainInterface $domain */
+ $options[$domain->id()] = $domain->label();
}
+
+ $element['domain_restriction'] = array(
+ '#type' => 'checkboxes',
+ '#title' => 'Visible on',
+ '#options' => $options,
+ '#default_value' => $plugin->getThirdPartySetting('domain_restriction_formatter', 'domain_restriction'),
+ '#description' => t('Limit the display of this field to the selected domains. If none are selected the field will be visible on all domains.'),
+ );
+
+ return $element;
}
/**
* Implements hook_field_formatter_settings_summary_alter().
*/
function domain_restriction_formatter_field_formatter_settings_summary_alter(&$summary, $context) {
- $display = $context['instance']['display'][$context['view_mode']];
- $settings = $display['settings'];
- $restrictions = array();
+ $settings = $context['formatter']->getThirdPartySetting('domain_restriction_formatter', 'domain_restriction');
+ $restrictions = !empty($settings) ? array_filter($settings) : [];
- if (!empty($summary)) {
- $summary .= '<br />';
- }
-
- // Check if there are domain restrictions.
- if (!empty($settings['domain_restriction_formatter'])) {
- $restrictions = array_filter($settings['domain_restriction_formatter']);
- }
if (!empty($restrictions)) {
- $summary .= t('<strong>There are domain restrictions in place.</strong>');
- }
- else {
- $summary .= t('No domain restrictions.');
- }
-}
+ $list = [
+ '#theme' => 'item_list',
+ '#items' => array_map(function ($domain_id) {
+ return Domain::load($domain_id)->label();
+ }, $restrictions),
+ '#type' => 'ul',
+ ];
-/**
- * Implements hook_field_formatter_settings_form_alter().
- */
-function domain_restriction_formatter_field_formatter_settings_form_alter(&$settings_form, $context) {
- $display = $context['instance']['display'][$context['view_mode']];
- $settings = $display['settings'];
-
- // Render checkboxes, so the user can decide on which domain(s) the field has to be show.
- $domains = domain_domains();
- $options = array();
- foreach ($domains as $key => $domain) {
- $options[$key] = $domain['subdomain'];
+ $summary[] = t('<strong>There are domain restrictions in place:</strong>' . \Drupal::service('renderer')
+ ->render($list));
}
- $settings_form['domain_restriction_formatter'] = array(
- '#type' => 'checkboxes',
- '#title' => 'Domains',
- '#options' => $options,
- '#default_value' => $settings['domain_restriction_formatter'],
- '#description' => t('Limit the display of this field to the selected domains. If none are selected the field will be visible on all domains.'),
- );
}
/**
- * Implements hook_preprocess_field().
- *
- * Unsets items if domain restrictions are in place.
+ * Implements hook_entity_view_alter().
*/
-function domain_restriction_formatter_preprocess_field(&$variables, $hook) {
- $entity_type = $variables['element']['#entity_type'];
- $field_name = $variables['element']['#field_name'];
- $bundle = $variables['element']['#bundle'];
- $view_mode = $variables['element']['#view_mode'];
-
- // Get the formatter info of the field.
- $formatter_info = field_formatter_settings_get_instance_display_settings($entity_type,
- $field_name, $bundle, $view_mode);
-
- if (!empty($formatter_info['domain_restriction_formatter'])) {
- $current_domain = domain_get_domain();
- $restrictions = array_filter($formatter_info['domain_restriction_formatter']);
- // If there are any domain restrictions, apply them.
- if (!empty($restrictions) && empty($formatter_info['domain_restriction_formatter'][$current_domain['domain_id']])) {
- $variables['items'] = array();
- $variables['label_hidden'] = TRUE;
+function domain_restriction_formatter_entity_view_alter(&$build, EntityInterface $entity, EntityDisplayInterface $display) {
+ $components = $display->getComponents();
+ // Implement domain restrictions.
+ foreach ($components as $field => $component) {
+ if (isset($component['third_party_settings']['domain_restriction_formatter']) && !empty($component['third_party_settings']['domain_restriction_formatter']['domain_restriction'])) {
+ if($restrictions = array_filter($component['third_party_settings']['domain_restriction_formatter']['domain_restriction'])){
+ /** @var \Drupal\domain\DomainNegotiatorInterface $negotiator */
+ $negotiator = Drupal::service('domain.negotiator');
+ if(!in_array($negotiator->getActiveDomain()->id(), $restrictions)){
+ // Hide field.
+ unset($build[$field]);
+ }
+ }
}
}
-}
+} \ No newline at end of file