/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $settings = $this->config('rest_api_doc.settings'); $enabled_route_names = $settings->get('routes'); $available_route_names = $this->state->get('rest_api_doc.rest_route_names'); if (empty($available_route_names)) { return array( 'no_routes' => array( '#markup' => $this->t('No REST enabled routes exist, please configure your REST end-points'), ), ); } else { $routes = $this->routeProvider->getRoutesByNames($available_route_names); $descriptions = array(); foreach ($routes as $route_name => $route) { $descriptions[$route_name] = $route_name . ' (' . $route->getPath() . ')'; } $form['routes'] = array( '#type' => 'checkboxes', '#title' => $this->t('Enabled routes'), '#description' => $this->t('Provide documentation for the following route names'), '#options' => array_combine($available_route_names, $descriptions), '#default_value' => $enabled_route_names, ); $form['overview'] = array( '#type' => 'textarea', '#default_value' => $settings->get('overview'), '#title' => $this->t('REST API overview'), '#description' => $this->t('Description to show on summary page. You may use site-wide tokens and some markup.'), ); } return parent::buildForm($form, $form_state); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('system.maintenance'); $form['maintenance_mode'] = array('#type' => 'checkbox', '#title' => t('Put site into maintenance mode'), '#default_value' => $this->state->get('system.maintenance_mode'), '#description' => t('Visitors will only see the maintenance mode message. Only users with the "Access site in maintenance mode" <a href=":permissions-url">permission</a> will be able to access the site. Authorized users can log in directly via the <a href=":user-login">user login</a> page.', array(':permissions-url' => $this->url('user.admin_permissions'), ':user-login' => $this->url('user.login')))); $form['maintenance_mode_message'] = array('#type' => 'textarea', '#title' => t('Message to display when in maintenance mode'), '#default_value' => $config->get('message')); return parent::buildForm($form, $form_state); }
/** * Reacts to the ConfigEvents::COLLECTION_NAMES event. * * @param \Drupal\Core\Config\ConfigCollectionInfo $collection_info * The configuration collection names event. */ public function addCollections(ConfigCollectionInfo $collection_info) { $collections = $this->state->get('config_collection_install_test.collection_names', array()); foreach ($collections as $collection) { $collection_info->addCollection($collection); } }
/** * Determines if redirect may be performed. * * @param Request $request * The current request object. * @param string $route_name * The current route name. * * @return bool * TRUE if redirect may be performed. */ public function canRedirect(Request $request, $route_name = NULL) { $can_redirect = TRUE; if (isset($route_name)) { $route = $this->routeProvider->getRouteByName($route_name); if ($this->config->get('access_check')) { // Do not redirect if is a protected page. $can_redirect &= $this->accessManager->check($route, $request, $this->account); } } else { $route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT); } if (strpos($request->getScriptName(), 'index.php') === FALSE) { // Do not redirect if the root script is not /index.php. $can_redirect = FALSE; } elseif (!($request->isMethod('GET') || $request->isMethod('HEAD'))) { // Do not redirect if this is other than GET request. $can_redirect = FALSE; } elseif ($this->state->get('system.maintenance_mode') || defined('MAINTENANCE_MODE')) { // Do not redirect in offline or maintenance mode. $can_redirect = FALSE; } elseif ($this->config->get('ignore_admin_path') && isset($route)) { // Do not redirect on admin paths. $can_redirect &= !(bool) $route->getOption('_admin_route'); } return $can_redirect; }
/** * Simple reporter log and display information about the queue. * * @param int $worker * Worker number. * @param object $item * The $item which was stored in the cron queue. */ protected function reportWork($worker, $item) { if ($this->state->get('cron_example_show_status_message')) { drupal_set_message($this->t('Queue @worker worker processed item with sequence @sequence created at @time', ['@worker' => $worker, '@sequence' => $item->sequence, '@time' => date_iso8601($item->created)])); } $this->logger->get('cron_example')->info('Queue @worker worker processed item with sequence @sequence created at @time', ['@worker' => $worker, '@sequence' => $item->sequence, '@time' => date_iso8601($item->created)]); }
/** * Alters base_route and parent_id into the views local tasks. */ public function alterLocalTasks(&$local_tasks) { $view_route_names = $this->state->get('views.view_route_names'); foreach ($this->getApplicableMenuViews() as $pair) { /** @var $executable \Drupal\views\ViewExecutable */ list($executable, $display_id) = $pair; $executable->setDisplay($display_id); $menu = $executable->display_handler->getOption('menu'); // We already have set the base_route for default tabs. if (in_array($menu['type'], array('tab'))) { $plugin_id = 'view.' . $executable->storage->id() . '.' . $display_id; $view_route_name = $view_route_names[$executable->storage->id() . '.' . $display_id]; // Don't add a local task for views which override existing routes. if ($view_route_name != $plugin_id) { unset($local_tasks[$plugin_id]); continue; } // Find out the parent route. // @todo Find out how to find both the root and parent tab. $path = $executable->display_handler->getPath(); $split = explode('/', $path); array_pop($split); $path = implode('/', $split); $pattern = '/' . str_replace('%', '{}', $path); if ($routes = $this->routeProvider->getRoutesByPattern($pattern)) { foreach ($routes->all() as $name => $route) { $local_tasks['views_view:' . $plugin_id]['base_route'] = $name; // Skip after the first found route. break; } } } } }
/** * Gets the private key. * * @return string * The private key. */ public function get() { if (!($key = $this->state->get('system.private_key'))) { $key = $this->create(); $this->set($key); } return $key; }
/** * {@inheritdoc} */ public function processInbound($path, Request $request) { if ($this->state->get('update_script_test_broken_inbound', FALSE)) { throw new \RuntimeException(); } else { return $path; } }
/** * {@inheritdoc} */ public function render(array $js_assets) { $elements = array(); // A dummy query-string is added to filenames, to gain control over // browser-caching. The string changes on every update or full cache // flush, forcing browsers to load a new copy of the files, as the // URL changed. Files that should not be cached (see _drupal_add_js()) // get REQUEST_TIME as query-string instead, to enforce reload on every // page request. $default_query_string = $this->state->get('system.css_js_query_string') ?: '0'; // For inline JavaScript to validate as XHTML, all JavaScript containing // XHTML needs to be wrapped in CDATA. To make that backwards compatible // with HTML 4, we need to comment out the CDATA-tag. $embed_prefix = "\n<!--//--><![CDATA[//><!--\n"; $embed_suffix = "\n//--><!]]>\n"; // Defaults for each SCRIPT element. $element_defaults = array('#type' => 'html_tag', '#tag' => 'script', '#value' => ''); // Loop through all JS assets. foreach ($js_assets as $js_asset) { // Element properties that do not depend on JS asset type. $element = $element_defaults; $element['#browsers'] = $js_asset['browsers']; // Element properties that depend on item type. switch ($js_asset['type']) { case 'setting': $element['#value_prefix'] = $embed_prefix; $element['#value'] = 'var drupalSettings = ' . Json::encode(drupal_merge_js_settings($js_asset['data'])) . ";"; $element['#value_suffix'] = $embed_suffix; break; case 'inline': $element['#value_prefix'] = $embed_prefix; $element['#value'] = $js_asset['data']; $element['#value_suffix'] = $embed_suffix; break; case 'file': $query_string = empty($js_asset['version']) ? $default_query_string : 'v=' . $js_asset['version']; $query_string_separator = strpos($js_asset['data'], '?') !== FALSE ? '&' : '?'; $element['#attributes']['src'] = file_create_url($js_asset['data']); // Only add the cache-busting query string if this isn't an aggregate // file. if (!isset($js_asset['preprocessed'])) { $element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : REQUEST_TIME); } break; case 'external': $element['#attributes']['src'] = $js_asset['data']; break; default: throw new \Exception('Invalid JS asset type.'); } // Attributes may only be set if this script is output independently. if (!empty($element['#attributes']['src']) && !empty($js_asset['attributes'])) { $element['#attributes'] += $js_asset['attributes']; } $elements[] = $element; } return $elements; }
/** * Loads menu path roots to prepopulate cache. */ protected function loadMenuPathRoots() { if ($roots = $this->state->get('router.path_roots')) { foreach ($roots as $root) { $this->storage[$root] = NULL; $this->persist($root); } } }
/** * Loads all non-admin routes right before the actual page is rendered. * * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event * The event to process. */ public function onRequest(KernelEvent $event) { // Only preload on normal HTML pages, as they will display menu links. if ($this->routeProvider instanceof PreloadableRouteProviderInterface && $event->getRequest()->getRequestFormat() == 'html') { if ($routes = $this->state->get('routing.non_admin_routes', [])) { // Preload all the non-admin routes at once. $this->routeProvider->preLoadRoutes($routes); } } }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { if ($row->isStub() && ($state = $this->state->get('comment.maintain_entity_statistics', 0))) { $this->state->set('comment.maintain_entity_statistics', 0); } $return = parent::import($row, $old_destination_id_values); if ($row->isStub() && $state) { $this->state->set('comment.maintain_entity_statistics', $state); } return $return; }
/** * {@inheritdoc} */ public function applies(RouteMatchInterface $route_match) { if (!$this->state->get('system.maintenance_mode')) { return FALSE; } if ($route = $route_match->getRouteObject()) { if ($route->getOption('_maintenance_access')) { return FALSE; } } return TRUE; }
/** * Gets the database connection object. * * @return \Drupal\Core\Database\Connection * The database connection. */ public function getDatabase() { if (!isset($this->database)) { // See if the database info is in state - if not, fallback to // configuration. if (isset($this->configuration['database_state_key'])) { $this->database = $this->setUpDatabase($this->state->get($this->configuration['database_state_key'])); } else { $this->database = $this->setUpDatabase($this->configuration); } } return $this->database; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['description'] = array('#markup' => '<p>' . t('Cron takes care of running periodic tasks like checking for updates and indexing content for search.') . '</p>'); $form['run'] = array('#type' => 'submit', '#value' => t('Run cron')); $status = '<p>' . $this->t('Last run: %time ago.', array('%time' => $this->dateFormatter->formatTimeDiffSince($this->state->get('system.cron_last')))) . '</p>'; $form['status'] = array('#markup' => $status); $cron_url = $this->url('system.cron', array('key' => $this->state->get('system.cron_key')), array('absolute' => TRUE)); $form['cron_url'] = array('#markup' => '<p>' . t('To run cron from outside the site, go to <a href=":cron">@cron</a>', array(':cron' => $cron_url, '@cron' => $cron_url)) . '</p>'); if (!$this->moduleHandler->moduleExists('automated_cron')) { $form['cron'] = array('#markup' => $this->t('Enable the <em>Automated Cron</em> module to allow cron execution at the end of a server response.')); } return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('system.cron'); $form['description'] = array('#markup' => '<p>' . t('Cron takes care of running periodic tasks like checking for updates and indexing content for search.') . '</p>'); $form['run'] = array('#type' => 'submit', '#value' => t('Run cron'), '#submit' => array(array($this, 'submitCron'))); $status = '<p>' . t('Last run: %cron-last ago.', array('%cron-last' => $this->dateFormatter->formatInterval(REQUEST_TIME - $this->state->get('system.cron_last')))) . '</p>'; $form['status'] = array('#markup' => $status); $form['cron_url'] = array('#markup' => '<p>' . t('To run cron from outside the site, go to <a href="!cron">!cron</a>', array('!cron' => url('cron/' . $this->state->get('system.cron_key'), array('absolute' => TRUE)))) . '</p>'); $form['cron'] = array('#title' => t('Cron settings'), '#type' => 'details', '#open' => TRUE); $options = array(3600, 10800, 21600, 43200, 86400, 604800); $form['cron']['cron_safe_threshold'] = array('#type' => 'select', '#title' => t('Run cron every'), '#description' => t('More information about setting up scheduled tasks can be found by <a href="@url">reading the cron tutorial on drupal.org</a>.', array('@url' => url('http://drupal.org/cron'))), '#default_value' => $config->get('threshold.autorun'), '#options' => array(0 => t('Never')) + array_map(array($this->dateFormatter, 'formatInterval'), array_combine($options, $options))); return parent::buildForm($form, $form_state); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $key = $input->getArgument('key'); if ($key) { $io->info($key); $io->writeln(Yaml::encode($this->state->get($key))); return 0; } $tableHeader = [$this->trans('commands.state.debug.messages.key')]; $keyStoreStates = array_keys($this->keyValue->get('state')->getAll()); $io->table($tableHeader, $keyStoreStates); return 0; }
/** * Dumps a set of routes to the router table in the database. * * Available options: * - provider: The route grouping that is being dumped. All existing * routes with this provider will be deleted on dump. * - base_class: The base class name. * * @param array $options * An array of options. */ public function dump(array $options = array()) { // Convert all of the routes into database records. // Accumulate the menu masks on top of any we found before. $masks = array_flip($this->state->get('routing.menu_masks.' . $this->tableName, array())); // Delete any old records first, then insert the new ones. That avoids // stale data. The transaction makes it atomic to avoid unstable router // states due to random failures. $transaction = $this->connection->startTransaction(); try { // We don't use truncate, because it is not guaranteed to be transaction // safe. try { $this->connection->delete($this->tableName)->execute(); } catch (\Exception $e) { $this->ensureTableExists(); } // Split the routes into chunks to avoid big INSERT queries. $route_chunks = array_chunk($this->routes->all(), 50, TRUE); foreach ($route_chunks as $routes) { $insert = $this->connection->insert($this->tableName)->fields(array('name', 'fit', 'path', 'pattern_outline', 'number_parts', 'route')); $names = array(); foreach ($routes as $name => $route) { /** @var \Symfony\Component\Routing\Route $route */ $route->setOption('compiler_class', '\\Drupal\\Core\\Routing\\RouteCompiler'); /** @var \Drupal\Core\Routing\CompiledRoute $compiled */ $compiled = $route->compile(); // The fit value is a binary number which has 1 at every fixed path // position and 0 where there is a wildcard. We keep track of all such // patterns that exist so that we can minimize the number of path // patterns we need to check in the RouteProvider. $masks[$compiled->getFit()] = 1; $names[] = $name; $values = array('name' => $name, 'fit' => $compiled->getFit(), 'path' => $route->getPath(), 'pattern_outline' => $compiled->getPatternOutline(), 'number_parts' => $compiled->getNumParts(), 'route' => serialize($route)); $insert->values($values); } // Insert all new routes. $insert->execute(); } } catch (\Exception $e) { $transaction->rollback(); watchdog_exception('Routing', $e); throw $e; } // Sort the masks so they are in order of descending fit. $masks = array_keys($masks); rsort($masks); $this->state->set('routing.menu_masks.' . $this->tableName, $masks); $this->routes = NULL; }
/** * {@inheritdoc} */ public function update(CommentInterface $comment) { // Allow bulk updates and inserts to temporarily disable the maintenance of // the {comment_entity_statistics} table. if (!$this->state->get('comment.maintain_entity_statistics')) { return; } $query = $this->database->select('comment_field_data', 'c'); $query->addExpression('COUNT(cid)'); $count = $query->condition('c.entity_id', $comment->getCommentedEntityId())->condition('c.entity_type', $comment->getCommentedEntityTypeId())->condition('c.field_name', $comment->getFieldName())->condition('c.status', CommentInterface::PUBLISHED)->condition('default_langcode', 1)->execute()->fetchField(); if ($count > 0) { // Comments exist. $last_reply = $this->database->select('comment_field_data', 'c')->fields('c', array('cid', 'name', 'changed', 'uid'))->condition('c.entity_id', $comment->getCommentedEntityId())->condition('c.entity_type', $comment->getCommentedEntityTypeId())->condition('c.field_name', $comment->getFieldName())->condition('c.status', CommentInterface::PUBLISHED)->condition('default_langcode', 1)->orderBy('c.created', 'DESC')->range(0, 1)->execute()->fetchObject(); // Use merge here because entity could be created before comment field. $this->database->merge('comment_entity_statistics')->fields(array('cid' => $last_reply->cid, 'comment_count' => $count, 'last_comment_timestamp' => $last_reply->changed, 'last_comment_name' => $last_reply->uid ? '' : $last_reply->name, 'last_comment_uid' => $last_reply->uid))->keys(array('entity_id' => $comment->getCommentedEntityId(), 'entity_type' => $comment->getCommentedEntityTypeId(), 'field_name' => $comment->getFieldName()))->execute(); } else { // Comments do not exist. $entity = $comment->getCommentedEntity(); // Get the user ID from the entity if it's set, or default to the // currently logged in user. if ($entity instanceof EntityOwnerInterface) { $last_comment_uid = $entity->getOwnerId(); } if (!isset($last_comment_uid)) { // Default to current user when entity does not implement // EntityOwnerInterface or author is not set. $last_comment_uid = $this->currentUser->id(); } $this->database->update('comment_entity_statistics')->fields(array('cid' => 0, 'comment_count' => 0, 'last_comment_timestamp' => $entity instanceof EntityChangedInterface ? $entity->getChangedTime() : REQUEST_TIME, 'last_comment_name' => '', 'last_comment_uid' => $last_comment_uid))->condition('entity_id', $comment->getCommentedEntityId())->condition('entity_type', $comment->getCommentedEntityTypeId())->condition('field_name', $comment->getFieldName())->execute(); } // Reset the cache of the commented entity so that when the entity is loaded // the next time, the statistics will be loaded again. $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->resetCache(array($comment->getCommentedEntityId())); }
/** * Retrieves the current state for a specific index. * * @param \Drupal\search_api\IndexInterface $index * The index whose state should be retrieved. * @param bool $provide_default * Whether to provide a default state array if there was no state saved. * * @return array|null * An associative array with information about the state of the given index: * - status: 0 if no tracking operation is currently in progress, 1 if there * is. * - page: If status is 1, the next page of items to process for tracking. * Or NULL if there was no saved state and $provide_default is FALSE. */ protected function getIndexState(IndexInterface $index, $provide_default = TRUE) { $default = array( 'status' => 0, 'pages' => array(), ); return $this->state->get($this->getIndexStateKey($index), $provide_default ? $default : NULL); }
/** * Regenerate the sitemap by setting the regenerate flag and running cron. */ protected function regenerateSitemap() { $this->state->set('xmlsitemap_regenerate_needed', TRUE); $this->state->set('xmlsitemap_generated_last', 0); $this->cronRun(); $this->assertTrue($this->state->get('xmlsitemap_generated_last') && !$this->state->get('xmlsitemap_regenerate_needed'), t('XML sitemaps regenerated and flag cleared.')); }
/** * Returns the entity_test hook invocation info. * * @return array * An associative array of arbitrary hook data keyed by hook name. */ protected function getHooksInfo() { $key = 'entity_test.hooks'; $hooks = $this->state->get($key); $this->state->set($key, array()); return $hooks; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->moduleHandler->loadInclude('locale', 'fetch.inc'); $this->moduleHandler->loadInclude('locale', 'bulk.inc'); $langcodes = array_filter($form_state->getValue('langcodes')); $projects = array_filter($form_state->getValue('projects_update')); // Set the translation import options. This determines if existing // translations will be overwritten by imported strings. $options = _locale_translation_default_update_options(); // If the status was updated recently we can immediately start fetching the // translation updates. If the status is expired we clear it an run a batch to // update the status and then fetch the translation updates. $last_checked = $this->state->get('locale.translation_last_checked'); if ($last_checked < REQUEST_TIME - LOCALE_TRANSLATION_STATUS_TTL) { locale_translation_clear_status(); $batch = locale_translation_batch_update_build(array(), $langcodes, $options); batch_set($batch); } else { // Set a batch to download and import translations. $batch = locale_translation_batch_fetch_build($projects, $langcodes, $options); batch_set($batch); // Set a batch to update configuration as well. if ($batch = locale_config_batch_update_components($options, $langcodes)) { batch_set($batch); } } }
/** * {@inheritdoc} * * This class evaluates the aggregation enabled/disabled condition on a group * by group basis by testing whether an aggregate file has been made for the * group rather than by testing the site-wide aggregation setting. This allows * this class to work correctly even if modules have implemented custom * logic for grouping and aggregating files. */ public function render(array $js_assets) { $elements = array(); // A dummy query-string is added to filenames, to gain control over // browser-caching. The string changes on every update or full cache // flush, forcing browsers to load a new copy of the files, as the // URL changed. Files that should not be cached get REQUEST_TIME as // query-string instead, to enforce reload on every page request. $default_query_string = $this->state->get('system.css_js_query_string') ?: '0'; // Defaults for each SCRIPT element. $element_defaults = array('#type' => 'html_tag', '#tag' => 'script', '#value' => ''); // Loop through all JS assets. foreach ($js_assets as $js_asset) { // Element properties that do not depend on JS asset type. $element = $element_defaults; $element['#browsers'] = $js_asset['browsers']; // Element properties that depend on item type. switch ($js_asset['type']) { case 'setting': $element['#attributes'] = array('type' => 'application/json', 'data-drupal-selector' => 'drupal-settings-json'); $element['#value'] = Json::encode($js_asset['data']); break; case 'file': $query_string = $js_asset['version'] == -1 ? $default_query_string : 'v=' . $js_asset['version']; $query_string_separator = strpos($js_asset['data'], '?') !== FALSE ? '&' : '?'; $element['#attributes']['src'] = file_url_transform_relative(file_create_url($js_asset['data'])); // Only add the cache-busting query string if this isn't an aggregate // file. if (!isset($js_asset['preprocessed'])) { $element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : REQUEST_TIME); } break; case 'external': $element['#attributes']['src'] = $js_asset['data']; break; default: throw new \Exception('Invalid JS asset type.'); } // Attributes may only be set if this script is output independently. if (!empty($element['#attributes']['src']) && !empty($js_asset['attributes'])) { $element['#attributes'] += $js_asset['attributes']; } $elements[] = $element; } return $elements; }
/** * Reacts to a config delete and records information in state for testing. * * @param \Drupal\Core\Config\ConfigCrudEvent $event */ public function onConfigDelete(ConfigCrudEvent $event) { $config = $event->getConfig(); if ($config->getName() == 'action.settings') { $value = $this->state->get('ConfigImportUITest.action.settings.delete', 0); $this->state->set('ConfigImportUITest.action.settings.delete', $value + 1); } }
/** * Retrieves the methods called on the test server. * * @param bool $reset * (optional) Whether to reset the list after the called methods are * retrieved. * * @return string[] * The methods called on the test server since the last reset. */ protected function getCalledServerMethods($reset = TRUE) { $key = 'search_api_test_backend.methods_called.' . $this->server->id(); $methods_called = $this->state->get($key, array()); if ($reset) { $this->state->delete($key); } return $methods_called; }
/** * Provides the sitemap in XML format. * * @throws NotFoundHttpException * * @return \Symfony\Component\HttpFoundation\Response * The sitemap in XML format or plain text if xmlsitemap_developer_mode flag * is set. */ public function renderSitemapXml() { $sitemap = $this->entityManager->getStorage('xmlsitemap')->loadByContext(); if (!$sitemap) { throw new NotFoundHttpException(); } $chunk = xmlsitemap_get_current_chunk($sitemap); $file = xmlsitemap_sitemap_get_file($sitemap, $chunk); // Provide debugging information if enabled. if ($this->state->get('xmlsitemap_developer_mode')) { $module_path = drupal_get_path('module', 'xmlsitemap'); $template = $this->twig->loadTemplate($module_path . '/templates/sitemap-developer-mode.html.twig'); $elements = array('current_context' => print_r($context, TRUE), 'sitemap' => print_r($sitemap, TRUE), 'chunk' => $chunk, 'cache_file_location' => $file, 'cache_file_exists' => file_exists($file) ? 'Yes' : 'No'); return new Response($template->render($elements)); } $response = new Response(); return xmlsitemap_output_file($response, $file); }
/** * Loads all non-admin routes right before the actual page is rendered. * * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event * The event to process. */ public function onRequest(KernelEvent $event) { // Only preload on normal HTML pages, as they will display menu links. if ($this->routeProvider instanceof PreloadableRouteProviderInterface && $event->getRequest()->getRequestFormat() == 'html') { // Ensure that the state query is cached to skip the database query, if // possible. $key = 'routing.non_admin_routes'; if ($cache = $this->cache->get($key)) { $routes = $cache->data; } else { $routes = $this->state->get($key, []); $this->cache->set($key, $routes, Cache::PERMANENT, ['routes']); } if ($routes) { // Preload all the non-admin routes at once. $this->routeProvider->preLoadRoutes($routes); } } }
/** * {@inheritdoc} */ public function loadByProperties(array $conditions = array()) { // Include deleted instances if specified in the $conditions parameters. $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE; unset($conditions['include_deleted']); $storages = array(); // Get field storages living in configuration. If we are explicitly looking // for deleted storages only, this can be skipped, because they will be // retrieved from state below. if (empty($conditions['deleted'])) { if (isset($conditions['entity_type']) && isset($conditions['field_name'])) { // Optimize for the most frequent case where we do have a specific ID. $id = $conditions['entity_type'] . $conditions['field_name']; $storages = $this->loadMultiple(array($id)); } else { // No specific ID, we need to examine all existing storages. $storages = $this->loadMultiple(); } } // Merge deleted field storages (living in state) if needed. if ($include_deleted || !empty($conditions['deleted'])) { $deleted_storages = $this->state->get('field.storage.deleted') ?: array(); foreach ($deleted_storages as $id => $config) { $storages[$id] = $this->create($config); } } // Collect matching fields. $matches = array(); foreach ($storages as $field) { foreach ($conditions as $key => $value) { // Extract the actual value against which the condition is checked. switch ($key) { case 'field_name': $checked_value = $field->name; break; case 'uuid': $checked_value = $field->uuid(); break; default: $checked_value = $field->{$key}; break; } // Skip to the next field as soon as one condition does not match. if ($checked_value != $value) { continue 2; } } // When returning deleted fields, key the results by UUID since they can // include several fields with the same ID. $key = $include_deleted ? $field->uuid() : $field->id(); $matches[$key] = $field; } return $matches; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $io->newLine(); $io->comment($this->trans('commands.node.access.rebuild.messages.rebuild')); $batch = $input->getOption('batch'); try { node_access_rebuild($batch); } catch (\Exception $e) { $io->error($e->getMessage()); return 1; } $needs_rebuild = $this->state->get('node.node_access_needs_rebuild') ?: false; if ($needs_rebuild) { $io->error($this->trans('commands.node.access.rebuild.messages.failed')); return 1; } $io->success($this->trans('commands.node.access.rebuild.messages.completed')); return 0; }