You are here

public function CourseObjectBook::save in Course 3.x

Same name and namespace in other branches
  1. 8.3 modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php \Drupal\course_book\Plugin\course\CourseObject\CourseObjectBook::save()
  2. 8.2 modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php \Drupal\course_book\Plugin\course\CourseObject\CourseObjectBook::save()

Override of CourseObjectNode::save()

We have to remove the stock "view" content access permissions on Books, if node_access_book is enabled. Otherwise, users outside of the course can still access child book pages of a private book parent.

Overrides CourseObject::save

File

modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php, line 167

Class

CourseObjectBook
Plugin annotation @CourseObject( id = "book", label = "Book", handlers = { "fulfillment" = "\Drupal\course_book\Plugin\course\CourseObject\CourseObjectBookFulfillment" } )

Namespace

Drupal\course_book\Plugin\course\CourseObject

Code

public function save() {

  // Take care of the parent book page.
  parent::save();
  if ($this
    ->hasNodePrivacySupport() && $this
    ->getOption('private') && Drupal::moduleHandler()
    ->moduleExists('node_access_book')) {

    // Remove "view" permissions on all the child pages.
    $flat = array();
    $tree = menu_tree_all_data($this
      ->getNode()->book['menu_name']);
    _book_flatten_menu($tree, $flat);
    foreach ($flat as $item) {
      $nid = str_replace('node/', '', $item['link_path']);
      $node = Node::load($nid);
      $settings = content_access_get_per_node_settings($node);
      $settings['view'] = array();
      content_access_save_per_node_settings($node, $settings);

      // Resave node to update access.
      node_access_acquire_grants($node);
    }
  }
}