public function access(Route $route, RouteMatch $match, AccountInterface $account) { $tempstore_id = $match->getParameter('tempstore_id') ? $match->getParameter('tempstore_id') : $route->getDefault('tempstore_id'); $id = $match->getParameter($route->getRequirement('_ctools_access')); if ($tempstore_id && $id) { $cached_values = $this->getTempstore()->get($tempstore_id)->get($id); if (!empty($cached_values['access']) && $cached_values['access'] instanceof CToolsAccessInterface) { $access = $cached_values['access']->access($account); } else { $access = AccessResult::allowed(); } } else { $access = AccessResult::forbidden(); } // The different wizards will have different tempstore ids and adding this // cache context allows us to nuance the access per wizard. $access->addCacheContexts(['url.query_args:tempstore_id']); return $access; }
public function access(Route $route, RouteMatch $match, AccountInterface $account) { $tempstore_id = $route->getDefault('tempstore_id'); $id = $match->getParameter($route->getRequirement('_ctools_access')); if ($tempstore_id && $id) { $cached_values = $this->getTempstore()->get($tempstore_id)->get($id); if (!empty($cached_values['access']) && $cached_values['access'] instanceof CToolsAccessInterface) { return $cached_values['access']->access($account); } } return AccessResult::forbidden(); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, RouteMatch $routeMatch = NULL) { $pbid = NULL; if ($routeMatch->getRouteName() == 'd8phonebook.edit') { // If we're on an edit URL, try to retrieve the phonebook entry by its ID. $pbid = $routeMatch->getParameter('phonebook'); $query = $this->connection->select('phonebook', 'p')->fields('p'); $query->condition('pbid', $pbid); $entry = $query->execute()->fetchObject(); if (!$entry) { // Return 404 if the entry is not found. throw new NotFoundHttpException(); } } // Push the phonebook entry ID to the submit handler. $form['pbid'] = ['#type' => 'value', '#value' => $pbid]; $form['name'] = ['#type' => 'textfield', '#title' => $this->t('Name'), '#maxlength' => 64, '#default_value' => $pbid ? $entry->name : '']; $form['phone'] = ['#type' => 'textfield', '#title' => $this->t('Phone'), '#maxlength' => 64, '#default_value' => $pbid ? $entry->phone : '']; $form['actions'] = ['#type' => 'actions', 'save' => ['#type' => 'submit', '#value' => $this->t('Save')]]; return $form; }
/** * Delete a phonebook entry. * * @param \Drupal\Core\Routing\RouteMatch $routeMatch */ public function delete(RouteMatch $routeMatch) { // CSRF is already checked by the routing system, so we're only making sure // that an existing item is being deleted. $pbid = $routeMatch->getParameter('phonebook'); $query = $this->connection->select('phonebook', 'p')->fields('p', ['name']); $query->condition('pbid', $pbid); $entry = $query->execute()->fetchObject(); if (!$entry) { // Return a 404 if the entry is not found. throw new NotFoundHttpException(); } $this->connection->delete('phonebook')->condition('pbid', $pbid)->execute(); drupal_set_message($this->t('Deleted entry for %name.', ['%name' => $entry->name])); return new RedirectResponse('/phonebook'); }