You are here

class Attachment in Views (for Drupal 7) 8.3

The plugin that handles an attachment display.

Attachment displays are secondary displays that are 'attached' to a primary display. Effectively they are a simple way to get multiple views within the same view. They can share some information.

Plugin annotation


@Plugin(
  id = "attachment",
  title = @Translation("Attachment"),
  help = @Translation("Attachments added to other displays to achieve multiple views in the same view."),
  theme = "views_view",
  contextual_links_locations = {""}
)

Hierarchy

Expanded class hierarchy of Attachment

Related topics

2 string references to 'Attachment'
views.view.glossary.yml in config/views.view.glossary.yml
config/views.view.glossary.yml
views.view.test_destroy.yml in tests/views_test_config/config/views.view.test_destroy.yml
tests/views_test_config/config/views.view.test_destroy.yml

File

lib/Drupal/views/Plugin/views/display/Attachment.php, line 30
Definition of Drupal\views\Plugin\views\display\Attachment.

Namespace

Drupal\views\Plugin\views\display
View source
class Attachment extends DisplayPluginBase {

  /**
   * Whether the display allows the use of a pager or not.
   *
   * @var bool
   */
  protected $usesPager = FALSE;
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['displays'] = array(
      'default' => array(),
    );
    $options['attachment_position'] = array(
      'default' => 'before',
    );
    $options['inherit_arguments'] = array(
      'default' => TRUE,
      'bool' => TRUE,
    );
    $options['inherit_exposed_filters'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['inherit_pager'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['render_pager'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    return $options;
  }
  public function execute() {
    return $this->view
      ->render($this->display['id']);
  }
  public function attachmentPositions($position = NULL) {
    $positions = array(
      'before' => t('Before'),
      'after' => t('After'),
      'both' => t('Both'),
    );
    if ($position) {
      return $positions[$position];
    }
    return $positions;
  }

  /**
   * Provide the summary for attachment options in the views UI.
   *
   * This output is returned as an array.
   */
  public function optionsSummary(&$categories, &$options) {

    // It is very important to call the parent function here:
    parent::optionsSummary($categories, $options);
    $categories['attachment'] = array(
      'title' => t('Attachment settings'),
      'column' => 'second',
      'build' => array(
        '#weight' => -10,
      ),
    );
    $displays = array_filter($this
      ->getOption('displays'));
    if (count($displays) > 1) {
      $attach_to = t('Multiple displays');
    }
    elseif (count($displays) == 1) {
      $display = array_shift($displays);
      if (!empty($this->view->display[$display])) {
        $attach_to = check_plain($this->view->display[$display]['display_title']);
      }
    }
    if (!isset($attach_to)) {
      $attach_to = t('Not defined');
    }
    $options['displays'] = array(
      'category' => 'attachment',
      'title' => t('Attach to'),
      'value' => $attach_to,
    );
    $options['attachment_position'] = array(
      'category' => 'attachment',
      'title' => t('Attachment position'),
      'value' => $this
        ->attachmentPositions($this
        ->getOption('attachment_position')),
    );
    $options['inherit_arguments'] = array(
      'category' => 'attachment',
      'title' => t('Inherit contextual filters'),
      'value' => $this
        ->getOption('inherit_arguments') ? t('Yes') : t('No'),
    );
    $options['inherit_exposed_filters'] = array(
      'category' => 'attachment',
      'title' => t('Inherit exposed filters'),
      'value' => $this
        ->getOption('inherit_exposed_filters') ? t('Yes') : t('No'),
    );
    $options['inherit_pager'] = array(
      'category' => 'pager',
      'title' => t('Inherit pager'),
      'value' => $this
        ->getOption('inherit_pager') ? t('Yes') : t('No'),
    );
    $options['render_pager'] = array(
      'category' => 'pager',
      'title' => t('Render pager'),
      'value' => $this
        ->getOption('render_pager') ? t('Yes') : t('No'),
    );
  }

  /**
   * Provide the default form for setting options.
   */
  public function buildOptionsForm(&$form, &$form_state) {

    // It is very important to call the parent function here:
    parent::buildOptionsForm($form, $form_state);
    switch ($form_state['section']) {
      case 'inherit_arguments':
        $form['#title'] .= t('Inherit contextual filters');
        $form['inherit_arguments'] = array(
          '#type' => 'checkbox',
          '#title' => t('Inherit'),
          '#description' => t('Should this display inherit its contextual filter values from the parent display to which it is attached?'),
          '#default_value' => $this
            ->getOption('inherit_arguments'),
        );
        break;
      case 'inherit_exposed_filters':
        $form['#title'] .= t('Inherit exposed filters');
        $form['inherit_exposed_filters'] = array(
          '#type' => 'checkbox',
          '#title' => t('Inherit'),
          '#description' => t('Should this display inherit its exposed filter values from the parent display to which it is attached?'),
          '#default_value' => $this
            ->getOption('inherit_exposed_filters'),
        );
        break;
      case 'inherit_pager':
        $form['#title'] .= t('Inherit pager');
        $form['inherit_pager'] = array(
          '#type' => 'checkbox',
          '#title' => t('Inherit'),
          '#description' => t('Should this display inherit its paging values from the parent display to which it is attached?'),
          '#default_value' => $this
            ->getOption('inherit_pager'),
        );
        break;
      case 'render_pager':
        $form['#title'] .= t('Render pager');
        $form['render_pager'] = array(
          '#type' => 'checkbox',
          '#title' => t('Render'),
          '#description' => t('Should this display render the pager values? This is only meaningful if inheriting a pager.'),
          '#default_value' => $this
            ->getOption('render_pager'),
        );
        break;
      case 'attachment_position':
        $form['#title'] .= t('Position');
        $form['attachment_position'] = array(
          '#type' => 'radios',
          '#description' => t('Attach before or after the parent display?'),
          '#options' => $this
            ->attachmentPositions(),
          '#default_value' => $this
            ->getOption('attachment_position'),
        );
        break;
      case 'displays':
        $form['#title'] .= t('Attach to');
        $displays = array();
        foreach ($this->view->display as $display_id => $display) {
          if (!empty($this->view->displayHandlers[$display_id]) && $this->view->displayHandlers[$display_id]
            ->acceptAttachments()) {
            $displays[$display_id] = $display['display_title'];
          }
        }
        $form['displays'] = array(
          '#type' => 'checkboxes',
          '#description' => t('Select which display or displays this should attach to.'),
          '#options' => $displays,
          '#default_value' => $this
            ->getOption('displays'),
        );
        break;
    }
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
  public function submitOptionsForm(&$form, &$form_state) {

    // It is very important to call the parent function here:
    parent::submitOptionsForm($form, $form_state);
    switch ($form_state['section']) {
      case 'inherit_arguments':
      case 'inherit_pager':
      case 'render_pager':
      case 'inherit_exposed_filters':
      case 'attachment_position':
      case 'displays':
        $this
          ->setOption($form_state['section'], $form_state['values'][$form_state['section']]);
        break;
    }
  }

  /**
   * Attach to another view.
   */
  public function attachTo($display_id) {
    $displays = $this
      ->getOption('displays');
    if (empty($displays[$display_id])) {
      return;
    }
    if (!$this
      ->access()) {
      return;
    }

    // Get a fresh view because our current one has a lot of stuff on it because it's
    // already been executed.
    $view = $this->view
      ->cloneView();
    $args = $this
      ->getOption('inherit_arguments') ? $this->view->args : array();
    $view
      ->setArguments($args);
    $view
      ->setDisplay($this->display['id']);
    if ($this
      ->getOption('inherit_pager')) {
      $view->display_handler->usesPager = $this->view->display[$display_id]->handler
        ->usesPager();
      $view->display_handler
        ->setOption('pager', $this->view->display[$display_id]->handler
        ->getOption('pager'));
    }
    $attachment = $view
      ->executeDisplay($this->display['id'], $args);
    switch ($this
      ->getOption('attachment_position')) {
      case 'before':
        $this->view->attachment_before .= $attachment;
        break;
      case 'after':
        $this->view->attachment_after .= $attachment;
        break;
      case 'both':
        $this->view->attachment_before .= $attachment;
        $this->view->attachment_after .= $attachment;
        break;
    }
    $view
      ->destroy();
  }

  /**
   * Attachment displays only use exposed widgets if
   * they are set to inherit the exposed filter settings
   * of their parent display.
   */
  public function usesExposed() {
    if (!empty($this->options['inherit_exposed_filters']) && parent::usesExposed()) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * If an attachment is set to inherit the exposed filter
   * settings from its parent display, then don't render and
   * display a second set of exposed filter widgets.
   */
  public function displaysExposed() {
    return $this->options['inherit_exposed_filters'] ? FALSE : TRUE;
  }
  public function renderPager() {
    return $this
      ->usesPager() && $this
      ->getOption('render_pager');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Attachment::$usesPager protected property Whether the display allows the use of a pager or not. Overrides DisplayPluginBase::$usesPager
Attachment::attachmentPositions public function
Attachment::attachTo public function Attach to another view. Overrides DisplayPluginBase::attachTo
Attachment::buildOptionsForm public function Provide the default form for setting options. Overrides DisplayPluginBase::buildOptionsForm
Attachment::defineOptions protected function Information about options for all kinds of purposes will be held here. @code 'option_name' => array( Overrides DisplayPluginBase::defineOptions
Attachment::displaysExposed public function If an attachment is set to inherit the exposed filter settings from its parent display, then don't render and display a second set of exposed filter widgets. Overrides DisplayPluginBase::displaysExposed
Attachment::execute public function When used externally, this is how a view gets run and returns data in the format required. Overrides DisplayPluginBase::execute
Attachment::optionsSummary public function Provide the summary for attachment options in the views UI. Overrides DisplayPluginBase::optionsSummary
Attachment::renderPager public function Not all display plugins will suppert pager rendering. Overrides DisplayPluginBase::renderPager
Attachment::submitOptionsForm public function Perform any necessary changes to the form values prior to storage. There is no need for this function to actually store the data. Overrides DisplayPluginBase::submitOptionsForm
Attachment::usesExposed public function Attachment displays only use exposed widgets if they are set to inherit the exposed filter settings of their parent display. Overrides DisplayPluginBase::usesExposed
DisplayPluginBase::$extender property Stores all available display extenders.
DisplayPluginBase::$handlers property
DisplayPluginBase::$output public property Stores the rendered output of the display.
DisplayPluginBase::$plugins protected property An array of instantiated plugins used in this display.
DisplayPluginBase::$usesAJAX protected property Whether the display allows the use of AJAX or not. 1
DisplayPluginBase::$usesAttachments protected property Whether the display allows attachments. 4
DisplayPluginBase::$usesMore protected property Whether the display allows the use of a 'more' link or not.
DisplayPluginBase::$usesOptions protected property Overrides Drupal\views\Plugin\Plugin::$usesOptions. Overrides PluginBase::$usesOptions
DisplayPluginBase::$view property The top object of a view. Overrides PluginBase::$view
DisplayPluginBase::acceptAttachments public function Determines whether this display can use attachments.
DisplayPluginBase::access public function Determine if the user has access to this display of the view.
DisplayPluginBase::defaultableSections public function Static member function to list which sections are defaultable and what items each section contains. 1
DisplayPluginBase::destroy public function Clears a plugin. Overrides PluginBase::destroy
DisplayPluginBase::formatThemes protected function Format a list of theme templates for output by the theme info helper.
DisplayPluginBase::getArgumentsTokens public function Returns to tokens for arguments.
DisplayPluginBase::getArgumentText public function Provide some helpful text for the arguments. The result should contain of an array with 1
DisplayPluginBase::getFieldLabels public function Retrieve a list of fields for the current display with the relationship associated if it exists.
DisplayPluginBase::getHandler public function Get the handler object for a single handler.
DisplayPluginBase::getHandlers public function Get a full array of handlers for $type. This caches them.
DisplayPluginBase::getLinkDisplay public function Check to see which display to use when creating links within a view using this display.
DisplayPluginBase::getOption public function Intelligently get an option either from this display or from the default display, if directed to do so.
DisplayPluginBase::getPagerText public function Provide some helpful text for pagers. 1
DisplayPluginBase::getPath public function Return the base path to use for this display.
DisplayPluginBase::getPlugin public function Get the instance of a plugin, for example style or row.
DisplayPluginBase::getSpecialBlocks public function Provide the block system with any exposed widget blocks for this display.
DisplayPluginBase::getStyleType protected function Displays can require a certain type of style plugin. By default, they will be 'normal'. 1
DisplayPluginBase::getUrl public function
DisplayPluginBase::hasPath public function Check to see if the display has a 'path' field. 1
DisplayPluginBase::hookBlockList public function If this display creates a block, implement one of these.
DisplayPluginBase::hookMenu public function If this display creates a page with a menu item, implement it here.
DisplayPluginBase::init public function 1
DisplayPluginBase::isAJAXEnabled public function Whether the display is actually using AJAX or not.
DisplayPluginBase::isDefaultDisplay public function Determine if this display is the 'default' display which contains fallback settings 1
DisplayPluginBase::isDefaulted public function Determine if a given option is set to use the default display or the current display
DisplayPluginBase::isEnabled public function Whether the display is enabled.
DisplayPluginBase::isIdentifierUnique public function Check if the provided identifier is unique.
DisplayPluginBase::isMoreEnabled public function Whether the display is using the 'more' link or not.
DisplayPluginBase::isPagerEnabled public function Whether the display is using a pager or not.
DisplayPluginBase::optionLink public function Because forms may be split up into sections, this provides an easy URL to exactly the right section. Don't override this.
DisplayPluginBase::optionsOverride public function If override/revert was clicked, perform the proper toggle.
DisplayPluginBase::overrideOption public function Set an option and force it to be an override.
DisplayPluginBase::preExecute public function Set up any variables on the view prior to execution. These are separated from execute because they are extremely common and unlikely to be overridden on an individual display.
DisplayPluginBase::preview function Fully render the display for the purposes of a live preview or some other AJAXy reason. 2
DisplayPluginBase::query public function Inject anything into the query that the display handler needs. Overrides PluginBase::query
DisplayPluginBase::render public function Render this display. 1
DisplayPluginBase::renderArea public function
DisplayPluginBase::renderEmpty public function
DisplayPluginBase::renderFilters public function Not all display plugins will support filtering
DisplayPluginBase::renderFooter public function Render the footer of the view.
DisplayPluginBase::renderHeader public function Render the header of the view.
DisplayPluginBase::renderMoreLink public function Render the 'more' link
DisplayPluginBase::setOption public function Intelligently set an option either from this display or from the default display, if directed to do so.
DisplayPluginBase::setOverride public function Flip the override setting for the given section.
DisplayPluginBase::useGroupBy public function Does the display have groupby enabled?
DisplayPluginBase::useMoreAlways public function Should the enabled display more link be shown when no more items?
DisplayPluginBase::useMoreText public function Does the display have custom link text?
DisplayPluginBase::usesAJAX public function Whether the display allows the use of AJAX or not. 1
DisplayPluginBase::usesAttachments public function Returns whether the display can use attachments. 4
DisplayPluginBase::usesBreadcrumb public function Check to see if the display needs a breadcrumb 1
DisplayPluginBase::usesExposedFormInBlock public function Check to see if the display can put the exposed formin a block.
DisplayPluginBase::usesFields public function Determine if the display's style uses fields.
DisplayPluginBase::usesLinkDisplay public function Check to see if the display has some need to link to another display. 1
DisplayPluginBase::usesMore public function Whether the display allows the use of a 'more' link or not.
DisplayPluginBase::usesPager public function Whether the display allows the use of a pager or not. 2
DisplayPluginBase::validate public function Make sure the display and all associated handlers are valid. Overrides PluginBase::validate 1
DisplayPluginBase::validateOptionsForm public function Validate the options form. Overrides PluginBase::validateOptionsForm 2
DisplayPluginBase::viewSpecialBlocks public function Render any special blocks provided for this display.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$definition public property Plugins's definition
PluginBase::$displayHandler public property The display object this plugin is for.
PluginBase::$options public property Options for this plugin will be held here.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::additionalThemeFunctions public function Provide a list of additional theme functions for the theme information page
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::pluginTitle public function Return the human readable name of the display.
PluginBase::setOptionDefaults protected function
PluginBase::summaryTitle public function Returns the summary of the settings in the display. 6
PluginBase::themeFunctions public function Provide a full list of possible theme templates used by this style. 1
PluginBase::unpackOptions public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
PluginBase::usesOptions public function Returns the usesOptions property. 8
PluginBase::__construct public function Constructs a Plugin object. Overrides PluginBase::__construct 2