class Attachment

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

5 string references to 'Attachment'
theme_file_formatter_table in drupal/core/modules/file/file.field.inc
Returns HTML for a file attachments table.
views.view.glossary.yml in drupal/core/modules/views/config/views.view.glossary.yml
drupal/core/modules/views/config/views.view.glossary.yml
views.view.test_attachment_ui.yml in drupal/core/modules/views/tests/views_test_config/test_views/views.view.test_attachment_ui.yml
drupal/core/modules/views/tests/views_test_config/test_views/views.view.test_attachment_ui.yml
views.view.test_destroy.yml in drupal/core/modules/views/tests/views_test_config/test_views/views.view.test_destroy.yml
drupal/core/modules/views/tests/views_test_config/test_views/views.view.test_destroy.yml
views.view.test_display_attachment.yml in drupal/core/modules/views/tests/views_test_config/test_views/views.view.test_display_attachment.yml
drupal/core/modules/views/tests/views_test_config/test_views/views.view.test_display_attachment.yml

File

drupal/core/modules/views/lib/Drupal/views/Plugin/views/display/Attachment.php, line 31
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 ($display = $this->view->storage
        ->getDisplay($display)) {
        $attach_to = check_plain($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->storage
          ->get('display') as $display_id => $display) {
          if ($this->view->displayHandlers
            ->has($display_id) && $this->view->displayHandlers
            ->get($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 'displays':
        $form_state['values'][$form_state['section']] = array_filter($form_state['values'][$form_state['section']]);
      case 'inherit_arguments':
      case 'inherit_pager':
      case 'render_pager':
      case 'inherit_exposed_filters':
      case 'attachment_position':
        $this
          ->setOption($form_state['section'], $form_state['values'][$form_state['section']]);
        break;
    }
  }

  /**
   * Attach to another view.
   */
  public function attachTo(ViewExecutable $view, $display_id) {
    $displays = $this
      ->getOption('displays');
    if (empty($displays[$display_id])) {
      return;
    }
    if (!$this
      ->access()) {
      return;
    }
    $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->displayHandlers
        ->get($display_id)
        ->usesPager();
      $view->display_handler
        ->setOption('pager', $this->view->displayHandlers
        ->get($display_id)
        ->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;
    }
  }

  /**
   * 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

Name Modifiers Type Descriptionsort ascending Overrides
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::isEnabled public function Whether the display is enabled.
DisplayPluginBase::ajaxEnabled public function Whether the display is actually using AJAX or not.
DisplayPluginBase::usesAJAX public function Whether the display allows the use of AJAX or not. 2
DisplayPluginBase::$usesAJAX protected property Whether the display allows the use of AJAX or not. 2
DisplayPluginBase::usesPager public function Whether the display allows the use of a pager or not. 4
Attachment::$usesPager protected property Whether the display allows the use of a pager or not. Overrides DisplayPluginBase::$usesPager
DisplayPluginBase::usesMore public function Whether the display allows the use of a 'more' link or not. 1
DisplayPluginBase::$usesMore protected property Whether the display allows the use of a 'more' link or not. 1
DisplayPluginBase::$usesAttachments protected property Whether the display allows attachments. 5
DisplayPluginBase::$usesAreas protected property Whether the display allows area plugins. 2
Attachment::execute public function When used externally, this is how a view gets run and returns data in the format required. Overrides DisplayPluginBase::execute
DisplayPluginBase::validateOptionsForm public function Validate the options form. Overrides PluginBase::validateOptionsForm 2
PluginBase::unpackOptions public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
DisplayPluginBase::$view property The top object of a view. Overrides PluginBase::$view
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$displayHandler public property The display object this plugin is for.
DisplayPluginBase::rescanThemes public function Submit hook to clear Drupal's theme registry (thereby triggering a templates rescan).
DisplayPluginBase::$output public property Stores the rendered output of the display.
DisplayPluginBase::$extender property Stores all available display extenders.
DisplayPluginBase::defaultableSections public function Static member function to list which sections are defaultable and what items each section contains. 1
DisplayPluginBase::useMoreAlways public function Should the enabled display more link be shown when no more items?
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::overrideOption public function Set an option and force it to be an override.
DisplayPluginBase::usesAttachments public function Returns whether the display can use attachments. 5
DisplayPluginBase::usesAreas public function Returns whether the display can use areas. 2
DisplayPluginBase::getArgumentsTokens public function Returns to tokens for arguments.
PluginBase::usesOptions public function Returns the usesOptions property. 8
PluginBase::summaryTitle public function Returns the summary of the settings in the display. 6
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
DisplayPluginBase::getType protected function Returns the display type that this display requires. 4
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getAvailableGlobalTokens public function Returns an array of available token replacements.
PluginBase::globalTokenReplace public function Returns a string with any core tokens replaced.
PluginBase::pluginTitle public function Return the human readable name of the display.
DisplayPluginBase::getPath public function Return the base path to use for this display. 1
DisplayPluginBase::getFieldLabels public function Retrieves a list of fields for the current display.
DisplayPluginBase::render public function Render this display. 3
DisplayPluginBase::viewExposedFormBlocks public function Render the exposed form as block.
DisplayPluginBase::renderMoreLink public function Render the 'more' link
DisplayPluginBase::renderArea public function Render one of the available areas.
DisplayPluginBase::remove public function Reacts on deleting a display. 1
Attachment::optionsSummary public function Provide the summary for attachment options in the views UI. Overrides DisplayPluginBase::optionsSummary
Attachment::buildOptionsForm public function Provide the default form for setting options. Overrides DisplayPluginBase::buildOptionsForm
DisplayPluginBase::getSpecialBlocks public function Provide the block system with any exposed widget blocks for this display.
DisplayPluginBase::getArgumentText public function Provide some helpful text for the arguments. The result should contain of an array with 1
DisplayPluginBase::getPagerText public function Provide some helpful text for pagers. 1
PluginBase::themeFunctions public function Provide a full list of possible theme templates used by this style. 1
PluginBase::$definition public property Plugins's definition
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
DisplayPluginBase::$usesOptions protected property Overrides Drupal\views\Plugin\Plugin::$usesOptions. Overrides PluginBase::$usesOptions 1
PluginBase::$options public property Options for this plugin will be held here.
DisplayPluginBase::renderFilters public function Not all display plugins will support filtering
Attachment::renderPager public function Not all display plugins will suppert pager rendering. Overrides DisplayPluginBase::renderPager
DisplayPluginBase::mergePlugin protected function Merges plugins default values.
DisplayPluginBase::mergeHandler protected function Merges handlers default values.
DisplayPluginBase::mergeDefaults public function Merges default values for all plugin types.
DisplayPluginBase::validate public function Make sure the display and all associated handlers are valid. Overrides PluginBase::validate 2
DisplayPluginBase::setOption public function Intelligently set an option either from this display or from the default display, if directed to do so.
DisplayPluginBase::getOption public function Intelligently get an option either from this display or from the default display, if directed to do so.
DisplayPluginBase::query public function Inject anything into the query that the display handler needs. Overrides PluginBase::query 1
PluginBase::init public function Initialize the plugin. 8
Attachment::defineOptions protected function Information about options for all kinds of purposes will be held here. @code 'option_name' => array( Overrides DisplayPluginBase::defineOptions
DisplayPluginBase::executeHookMenu public function If this display creates a page with a menu item, implement it here. 1
DisplayPluginBase::optionsOverride public function If override/revert was clicked, perform the proper toggle.
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
DisplayPluginBase::getPlugin public function Get the instance of a plugin, for example style or row.
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::preview function Fully render the display for the purposes of a live preview or some other AJAXy reason. 3
DisplayPluginBase::formatThemes protected function Format a list of theme templates for output by the theme info helper.
DisplayPluginBase::setOverride public function Flip the override setting for the given section.
DisplayPluginBase::getAttachedDisplays public function Find out all displays which are attached to this display.
PluginBase::setOptionDefaults protected function Fills up the options of the plugin with defaults.
DisplayPluginBase::useGroupBy public function Does the display have groupby enabled?
DisplayPluginBase::useMoreText public function Does the display have custom link text?
DisplayPluginBase::changeThemeForm public function Displays the Change Theme form.
DisplayPluginBase::acceptAttachments public function Determines whether this display can use attachments.
DisplayPluginBase::isDefaultDisplay public function Determine if this display is the 'default' display which contains fallback settings 1
DisplayPluginBase::access public function Determine if the user has access to this display of the view.
DisplayPluginBase::usesFields public function Determine if the display's style uses fields.
DisplayPluginBase::isDefaulted public function Determine if a given option is set to use the default display or the current display
ContainerFactoryPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 11
PluginBase::__construct public function Constructs a Plugin object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
DisplayPluginBase::destroy public function Clears a plugin. Overrides PluginBase::destroy
DisplayPluginBase::getLinkDisplay public function Check to see which display to use when creating links within a view using this display.
DisplayPluginBase::usesBreadcrumb public function Check to see if the display needs a breadcrumb 1
DisplayPluginBase::usesLinkDisplay public function Check to see if the display has some need to link to another display. 1
DisplayPluginBase::hasPath public function Check to see if the display has a 'path' field. 1
DisplayPluginBase::usesExposedFormInBlock public function Check to see if the display can put the exposed formin a block.
DisplayPluginBase::isIdentifierUnique public function Check if the provided identifier is unique.
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.
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
Attachment::attachTo public function Attach to another view. Overrides DisplayPluginBase::attachTo
DisplayPluginBase::$plugins protected property An array of instantiated plugins used in this display.
PluginBase::globalTokenForm public function Adds elements for available core tokens to a form.
Attachment::attachmentPositions public function
DisplayPluginBase::initDisplay public function 2
DisplayPluginBase::getUrl public function
DisplayPluginBase::$handlers property