You are here

protected function QuickSet::prepareContents in Quick Tabs 7.3

This method does some initial set-up of the tab contents, such as hiding tabs with no content if the hide_empty_tabs option is set. It also makes sure that prerendered contents are never attempted to be loaded via ajax.

Throws

InvalidQuickSetException if there are no contents to render.

1 call to QuickSet::prepareContents()
QuickSet::__construct in ./quicktabs.classes.inc
Constructor

File

./quicktabs.classes.inc, line 182

Class

QuickSet
A QuickSet object is an unrendered Quicktabs instance, essentially just a container of content items, as defined by its configuration settings and the array of content items it contains.

Code

protected function prepareContents() {
  if (!count($this->contents)) {
    throw new InvalidQuickSetException('There are no contents to render.');
  }
  if ($this->settings['hide_empty_tabs'] && !$this->settings['ajax']) {

    // Check if any tabs need to be hidden because of empty content.
    $renderable_contents = 0;
    foreach ($this->contents as $key => $tab) {
      $contents = $tab
        ->render(TRUE);
      reset($contents);
      if (!empty($contents)) {

        // If it's block type quicktab, #markup will be on level lower.
        if (!isset($contents['#markup'])) {
          $contents = reset($contents);
        }
        $string = strip_tags($contents['#markup']);

        // If string contains only whitespaces it will be removed.
        $string = trim($string);
      }
      else {
        $string = $contents;
      }
      if (!$string) {

        // Rather than removing the item, we set it to NULL. This way we retain
        // the same indices across tabs, so that permanent links to particular
        // tabs can be relied upon.
        $this->contents[$key] = NULL;

        // The default tab must not be a hidden tab.
        if ($this->settings['default_tab'] == $key) {
          $this->settings['default_tab'] = ($key + 1) % count($this->contents);
        }
      }
      else {
        $renderable_contents++;
      }
    }
    if (!$renderable_contents) {
      throw new InvalidQuickSetException('There are no contents to render.');
    }
  }
  elseif ($this->settings['ajax']) {

    // Make sure that there is at most 1 prerendered tab and it is the default tab.
    // Prerendered content cannot be rendered via ajax.
    $has_prerendered = FALSE;

    // keep track of whether we have found a prerendered tab.
    foreach ($this->contents as $key => $tab) {
      $type = $tab
        ->getType();
      if ($type == 'prerendered') {
        if (!$has_prerendered) {
          $has_prerendered = TRUE;
          $this->settings['default_tab'] = $key;

          // In the case of a direct link to a different tab, the 'default_tab'
          // will be overridden, so we need to make sure it does not attempt
          // to load a pre-rendered tab via ajax. Turn ajax option off.
          if ($this
            ->getActiveTab() !== $key) {
            $this->settings['ajax'] = 0;
          }
        }
        else {

          // We are on a second custom tab and the ajax option is set, we cannot
          // render custom tabs via ajax, so we skip out of the loop, set the
          // ajax option to off, and call the method again.
          $this->settings['ajax'] = 0;
          $this
            ->prepareContents();
          return;
        }
      }
    }
  }
}