function View::addDisplay

Same name in other branches
  1. 9 core/modules/views/src/Entity/View.php \Drupal\views\Entity\View::addDisplay()
  2. 8.9.x core/modules/views/src/Entity/View.php \Drupal\views\Entity\View::addDisplay()
  3. 10 core/modules/views/src/Entity/View.php \Drupal\views\Entity\View::addDisplay()

Overrides ViewEntityInterface::addDisplay

File

core/modules/views/src/Entity/View.php, line 148

Class

View
Defines a View configuration entity class.

Namespace

Drupal\views\Entity

Code

public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
    if (empty($plugin_id)) {
        return FALSE;
    }
    $plugin = Views::pluginManager('display')->getDefinition($plugin_id);
    if (empty($plugin)) {
        $plugin['title'] = t('Broken');
    }
    if (empty($id)) {
        $id = $this->generateDisplayId($plugin_id);
        // Generate a unique human-readable name by inspecting the counter at the
        // end of the previous display ID, e.g., 'page_1'.
        if ($id !== 'default') {
            preg_match("/[0-9]+/", $id, $count);
            $count = $count[0];
        }
        else {
            $count = '';
        }
        if (empty($title)) {
            // If there is no title provided, use the plugin title, and if there are
            // multiple displays, append the count.
            $title = $plugin['title'];
            if ($count > 1) {
                $title .= ' ' . $count;
            }
        }
    }
    $display_options = [
        'display_plugin' => $plugin_id,
        'id' => $id,
        // Cast the display title to a string since it is an object.
        // @see \Drupal\Core\StringTranslation\TranslatableMarkup
'display_title' => (string) $title,
        'position' => $id === 'default' ? 0 : count($this->display),
        'display_options' => [],
    ];
    // Add the display options to the view.
    $this->display[$id] = $display_options;
    return $id;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.