You are here

public function DashboardLayoutBuilderForm::resetToDefault in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x src/Form/DashboardLayoutBuilderForm.php \Drupal\dashboards\Form\DashboardLayoutBuilderForm::resetToDefault()

Reset to default layout.

Parameters

array $form: Form.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

File

src/Form/DashboardLayoutBuilderForm.php, line 132

Class

DashboardLayoutBuilderForm
DashboardLayoutBuilderForm class.

Namespace

Drupal\dashboards\Form

Code

public function resetToDefault(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\dashboards\Entity\Dashboard $dashboard */
  $dashboard = $this->sectionStorage
    ->getContextValue(Dashboard::CONTEXT_TYPE);

  // Delete the userdata containing the dashboard override.
  $this->userData
    ->delete('dashboards', $this->account
    ->id(), $dashboard
    ->id());

  // Delete the tempstore so the override form is reloaded as well.
  $this->layoutTempstoreRepository
    ->delete($this->sectionStorage);

  // Create a new trusted redirect response.
  $response = new TrustedRedirectResponse(Url::fromRoute('entity.dashboard.canonical', [
    'dashboard' => $dashboard
      ->id(),
  ])
    ->toString());

  // Set cacheable metadata.
  $metadata = $response
    ->getCacheableMetadata();
  $metadata
    ->setCacheTags($dashboard
    ->getCacheTags());
  $metadata
    ->setCacheContexts($dashboard
    ->getCacheContexts());

  // Also invalidate the cache.
  Cache::invalidateTags($dashboard
    ->getCacheTags());

  // Set the response so we're redirected back to the dashboard.
  $form_state
    ->setResponse($response);
}