You are here

function _viewreference_get_applicable_views_displays in View reference 7.3

Parameters

array $settings: If applying filters, the settings of the field.

Return value

array[] An array where the keys are views machine names, and each value is an array of the format array($view, $applicable_displays), where the first element is a view, and the second element is an array of views displays.

2 calls to _viewreference_get_applicable_views_displays()
viewreference_get_views in ./viewreference.module
Get an array of views.
_viewreference_select_widget_options in ./viewreference.module

File

./viewreference.module, line 641
Defines a field type for referencing a view from a node.

Code

function _viewreference_get_applicable_views_displays($settings) {
  $loaded_views = views_get_all_views();

  // Prepare filters.
  if (!empty($settings['referenceable_views'])) {
    $filters['ids'] = array_filter($settings['referenceable_views']);
  }
  if (!empty($settings['referenceable_tags']['allow'])) {
    $filters['allow_tags'] = array_map('trim', explode(',', $settings['referenceable_tags']['allow']));
  }
  if (!empty($settings['referenceable_tags']['deny'])) {
    $filters['deny_tags'] = array_map('trim', explode(',', $settings['referenceable_tags']['deny']));
  }
  $applicable_views_displays = array();
  foreach ((array) $loaded_views as $view_name => $view) {

    // Limit views to active views
    if (!empty($settings['skip_disabled']) && $view->disabled) {
      continue;
    }

    // Prepare this view's tags.
    $tags = array_map('trim', explode(',', $view->tag));

    // Determine if there are allow tags
    $has_allow_tag = FALSE;
    if (!empty($tags) && !empty($filters['allow_tags'])) {
      foreach ($tags as $tag) {
        if (in_array($tag, $filters['allow_tags'])) {
          $has_allow_tag = TRUE;
          break;
        }
      }
    }

    // Determine if there are deny tags
    $has_deny_tag = FALSE;
    if (!empty($tags) && !empty($filters['deny_tags'])) {
      foreach ($tags as $tag) {
        if (in_array($tag, $filters['deny_tags'])) {
          $has_deny_tag = TRUE;
          break;
        }
      }
    }

    // Skip the entire view, if there is a deny tag.
    if ($has_deny_tag) {
      continue;
    }
    $allow_all = $has_allow_tag || empty($filters['ids']) && empty($filters['allow_tags']);
    $applicable_displays = array();
    foreach ((array) $view->display as $display_key => $display) {

      // Skip this one if it's a 'default' view and we're skipping defaults.
      if ($display_key === 'default' && $settings['skip_default']) {
        continue;
      }
      if ($allow_all) {
        $applicable_displays[$display_key] = $display;
        continue;
      }

      // Skip this one if it's not 'allowed'.
      $id = $view_name . ':' . $display_key;
      if (!empty($filters['ids']) && in_array($id, $filters['ids'])) {
        $applicable_displays[$display_key] = $display;
      }
    }
    if (!empty($applicable_displays)) {
      $applicable_views_displays[$view_name] = array(
        $view,
        $applicable_displays,
      );
    }
  }
  return $applicable_views_displays;
}