views_invalidate_cache example

// Restore the previous active display.     $executable->setDisplay($current_display);
  }

  /** * {@inheritdoc} */
  public function postSave(EntityStorageInterface $storage$update = TRUE) {
    parent::postSave($storage$update);

    // @todo Remove if views implements a view_builder controller.     views_invalidate_cache();
    $this->invalidateCaches();

    // Rebuild the router if this is a new view, or its status changed.     if (!isset($this->original) || ($this->status() != $this->original->status())) {
      \Drupal::service('router.builder')->setRebuildNeeded();
    }
  }

  /** * {@inheritdoc} */
  
->set('sql_signature', $form_state->getValue('sql_signature'))
      ->set('display_extenders', $form_state->getValue('display_extenders', []))
      ->save();

    parent::submitForm($form$form_state);
  }

  /** * Submission handler to clear the Views cache. */
  public function cacheSubmit() {
    views_invalidate_cache();
    $this->messenger()->addStatus($this->t('The cache has been cleared.'));
  }

}
// Change the page plugin id to an invalid one. Bypass the entity system     // so no menu rebuild was executed (so the path is still available).     $config = $this->config('views.view.test_display_invalid');
    $config->set('display.page_1.display_plugin', 'invalid');
    $config->save();

    $this->drupalGet('test_display_invalid');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('The "invalid" plugin does not exist.');

    // Rebuild the router, and ensure that the path is not accessible anymore.     views_invalidate_cache();
    \Drupal::service('router.builder')->rebuildIfNeeded();

    $this->drupalGet('test_display_invalid');
    $this->assertSession()->statusCodeEquals(404);

    // Change the display plugin ID back to the correct ID.     $config = $this->config('views.view.test_display_invalid');
    $config->set('display.page_1.display_plugin', 'page');
    $config->save();

    // Place the block display.
Home | Imprint | This part of the site doesn't use cookies.