function View::duplicateDisplayAsType

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

Overrides ViewEntityInterface::duplicateDisplayAsType

File

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

Class

View
Defines a View configuration entity class.

Namespace

Drupal\views\Entity

Code

public function duplicateDisplayAsType($old_display_id, $new_display_type) {
    $executable = $this->getExecutable();
    $display = $executable->newDisplay($new_display_type);
    $new_display_id = $display->display['id'];
    $displays = $this->get('display');
    // Let the display title be generated by the addDisplay method and set the
    // right display plugin, but keep the rest from the original display.
    $display_duplicate = $displays[$old_display_id];
    unset($display_duplicate['display_title']);
    unset($display_duplicate['display_plugin']);
    unset($display_duplicate['new_id']);
    $displays[$new_display_id] = NestedArray::mergeDeep($displays[$new_display_id], $display_duplicate);
    $displays[$new_display_id]['id'] = $new_display_id;
    // First set the displays.
    $this->set('display', $displays);
    // Ensure that we just copy display options, which are provided by the new
    // display plugin.
    $executable->setDisplay($new_display_id);
    $executable->display_handler
        ->filterByDefinedOptions($displays[$new_display_id]['display_options']);
    // Update the display settings.
    $this->set('display', $displays);
    return $new_display_id;
}

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