diff --git a/README.txt b/README.md similarity index 100% rename from README.txt rename to README.md diff --git a/domain_restriction_formatter.info b/domain_restriction_formatter.info deleted file mode 100644 index 6f77c0a387845758d8cb1ff597797897a42b2d3b..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..dd57ebd74c41867ab05957fa6a5043f5880bf068 --- /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 dd347edeeb32e6858d2b6187080001b33b23c711..b65237c096edee522a5e6641d3d8b705c69baf9e 100644 --- a/domain_restriction_formatter.module +++ b/domain_restriction_formatter.module @@ -1,96 +1,85 @@ ' . $output . ''; +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 '
' . $output . '
'; + 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 .= '
'; - } - - // Check if there are domain restrictions. - if (!empty($settings['domain_restriction_formatter'])) { - $restrictions = array_filter($settings['domain_restriction_formatter']); - } if (!empty($restrictions)) { - $summary .= t('There are domain restrictions in place.'); - } - 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('There are domain restrictions in place:' . \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