Skip to content
domain_restriction_formatter.module 3.25 KiB
Newer Older
Robin Ingelbrecht's avatar
Robin Ingelbrecht committed
<?php
/**
 * Holds hooks and general functionality.
 */

/**
 * 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>';
  }
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function domain_restriction_formatter_field_formatter_info_alter(&$info) {
  foreach ($info as &$formatter) {
    $formatter['settings']['domain_restriction_formatter'] = '';
  }
}

/**
 * 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();

  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.');
  }
}

/**
 * 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'];
  }
  $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.
 */
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;
    }
  }
}