diff --git a/plugins/facetapi/widget_links.inc b/plugins/facetapi/widget_links.inc index 81075b2..b665d1d 100644 --- a/plugins/facetapi/widget_links.inc +++ b/plugins/facetapi/widget_links.inc @@ -14,6 +14,13 @@ class FacetapiWidgetLinks extends FacetapiWidget { /** + * Whether to put rel=nofollow attribute on active and inactive facet items. + * + * @var bool + */ + protected $nofollowActive, $nofollowInactive; + + /** * Overrides FacetapiWidget::__construct(). * * For links, it is better to use the machine name of the facet as opposed to @@ -87,10 +94,7 @@ class FacetapiWidgetLinks extends FacetapiWidget { */ function buildListItems($build) { $settings = $this->settings->settings; - - // Initializes links attributes, adds rel="nofollow" if configured. - $attributes = ($settings['nofollow']) ? array('rel' => 'nofollow') : array(); - $attributes += array('class' => $this->getItemClasses()); + $attributes = array('class' => $this->getItemClasses()); // Builds rows. $items = array(); @@ -109,6 +113,10 @@ class FacetapiWidgetLinks extends FacetapiWidget { ), ); + if ($this->itemGetsNoFollow($item)) { + $variables['options']['attributes']['rel'] = 'nofollow'; + } + // Adds the facetapi-zero-results class to items that have no results. if (!$item['#count']) { $variables['options']['attributes']['class'][] = 'facetapi-zero-results'; @@ -146,6 +154,32 @@ class FacetapiWidgetLinks extends FacetapiWidget { } /** + * Determines whether the rel attribute should be set to nofollow for an item. + * + * @param $item + * The current item. + * + * @return bool + * Whether the rel attribute should be set to nofollow. + */ + function itemGetsNoFollow($item) { + + if (!isset($this->nofollowActive)) { + $settings = $this->settings->settings; + // BC support. + if (empty($settings['nofollow_depth'])) { + $settings['nofollow_depth'] = !empty($settings['nofollow']) ? 0 : 100; + } + + $current_depth = count($this->facet->getAdapter()->getAllActiveItems()); + $this->nofollowInactive = $current_depth + 1 > $settings['nofollow_depth']; + $this->nofollowActive = $current_depth - 1 > $settings['nofollow_depth']; + } + + return ($item['#active'] && $this->nofollowActive || !$item['#active'] && $this->nofollowInactive); + } + + /** * Gets the base class array for a facet item. * * Classes that extend FacetapiWidgetLinks will often overide this method to @@ -179,12 +213,20 @@ class FacetapiWidgetLinks extends FacetapiWidget { ), ); + $options = array('0' => t('All links')); + for ($n = 1; $n <= 10; $n++) { + $options[$n] = t('for >@n active facets', array('@n' => $n)); + } + $options[100] = t('disabled'); + // @see http://drupal.org/node/1370342 - $form['widget']['widget_settings']['links'][$this->id]['nofollow'] = array( - '#type' => 'checkbox', - '#title' => t('Prevent crawlers from following facet links'), - '#default_value' => !empty($this->settings->settings['nofollow']), - '#description' => t('Add the rel="nofollow" attribute to facet links to maximize SEO by preventing crawlers from indexing duplicate content and getting stuck in loops.'), + $form['widget']['widget_settings']['links'][$this->id]['nofollow_depth'] = array( + '#type' => 'select', + '#title' => t('Set nofollow on facet links'), + // We support the nofollow setting for BC. + '#default_value' => isset($this->settings->settings['nofollow_depth']) ? $this->settings->settings['nofollow_depth'] : (!empty($this->settings->settings['nofollow']) ? 0 : 100), + '#description' => t('Adding the rel="nofollow" attribute to facet links to maximize SEO by preventing crawlers from indexing duplicate content and getting stuck in loops.'), + '#options' => $options, '#states' => array( 'visible' => array( 'select[name="widget"]' => array('value' => $this->id),