/**
  * {@inheritdoc}
  */
 public function render(array $render_array)
 {
     $content = $this->drupalRenderRoot($render_array);
     if (!empty($render_array)) {
         drupal_process_attached($render_array);
     }
     $cache = !empty($render_array['#cache']['tags']) ? ['tags' => $render_array['#cache']['tags']] : [];
     $fragment = new HtmlFragment($content, $cache);
     if (isset($render_array['#title'])) {
         $fragment->setTitle($render_array['#title'], Title::FILTER_XSS_ADMIN);
     }
     $attached = isset($render_array['#attached']) ? $render_array['#attached'] : [];
     $attached += ['feed' => [], 'html_head' => [], 'html_head_link' => []];
     // Add feed links from the page content.
     foreach ($attached['feed'] as $feed) {
         $fragment->addLinkElement(new FeedLinkElement($feed[1], $this->urlGenerator->generateFromPath($feed[0])));
     }
     // Add generic links from the page content.
     foreach ($attached['html_head_link'] as $link) {
         $fragment->addLinkElement(new LinkElement($this->urlGenerator->generateFromPath($link[0]['href']), $link[0]['rel']));
     }
     // @todo Also transfer the contents of "_drupal_add_html_head" once
     // https://www.drupal.org/node/2296951 lands.
     // @todo Transfer CSS and JS over to the fragment once those are supported
     // on the fragment object.
     return $fragment;
 }
 /**
  * Converts a render array into an HtmlFragment object.
  *
  * @param array|string $page_content
  *   The page content area to display.
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request object.
  *
  * @return \Drupal\Core\Page\HtmlPage
  *   A page object.
  */
 protected function createHtmlFragment($page_content, Request $request)
 {
     // Allow controllers to return a HtmlFragment or a Response object directly.
     if ($page_content instanceof HtmlFragment || $page_content instanceof Response) {
         return $page_content;
     }
     if (!is_array($page_content)) {
         $page_content = array('main' => array('#markup' => $page_content));
     }
     $content = $this->drupalRender($page_content);
     $cache = !empty($page_content['#cache']['tags']) ? array('tags' => $page_content['#cache']['tags']) : array();
     $fragment = new HtmlFragment($content, $cache);
     // A title defined in the return always wins.
     if (isset($page_content['#title'])) {
         $fragment->setTitle($page_content['#title'], Title::FILTER_XSS_ADMIN);
     } else {
         if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
             $fragment->setTitle($this->titleResolver->getTitle($request, $route), Title::PASS_THROUGH);
         }
     }
     // Add feed links from the page content.
     $attached = drupal_render_collect_attached($page_content, TRUE);
     if (!empty($attached['drupal_add_feed'])) {
         foreach ($attached['drupal_add_feed'] as $feed) {
             $feed_link = new FeedLinkElement($feed[1], $this->urlGenerator->generateFromPath($feed[0]));
             $fragment->addLinkElement($feed_link);
         }
     }
     return $fragment;
 }
 /**
  * {@inheritdoc}
  */
 public function execute($object = NULL)
 {
     $url = $this->urlGenerator->generateFromPath($this->configuration['url'], array('absolute' => TRUE));
     $response = new RedirectResponse($url);
     $listener = function ($event) use($response) {
         $event->setResponse($response);
     };
     // Add the listener to the event dispatcher.
     $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener);
 }
 /**
  * {@inheritdoc}
  */
 public function listAttribute($content)
 {
     $this->crawler->addContent($content);
     $this->links = array();
     $this->crawler->filter('a')->each(function (HtmlPageCrawler $anchor, $uri) {
         $href = $anchor->attr('href');
         // @todo deprecated method.
         $this->links[] = $this->urlGenerator->generateFromPath($href, array('absolute' => TRUE));
     });
     $this->crawler->remove();
     return implode(',', $this->links);
 }
 /**
  * {@inheritdoc}
  */
 public function generateFromPath($path = NULL, $options = array(), $collect_bubbleable_metadata = FALSE)
 {
     $generated_url = $this->urlGenerator->generateFromPath($path, $options, TRUE);
     if (!$collect_bubbleable_metadata) {
         $this->bubble($generated_url, $options);
     }
     return $collect_bubbleable_metadata ? $generated_url : $generated_url->getGeneratedUrl();
 }
Exemple #6
0
 /**
  * Generates an absolute URL given a path.
  *
  * @param string $path
  *   The path.
  * @param array $options
  *   (optional) An associative array of additional options. The 'absolute'
  *   option is forced to be TRUE.
  *
  * @return string
  *   The generated absolute URL for the given path.
  *
  * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 8.0.0.
  */
 public function getUrlFromPath($path, $options = array())
 {
     // Generate URL.
     $options['absolute'] = TRUE;
     $generated_url = $this->urlGenerator->generateFromPath($path, $options, TRUE);
     // Return as render array, so we can bubble the bubbleable metadata.
     $build = ['#markup' => $generated_url->getGeneratedUrl()];
     $generated_url->applyTo($build);
     return $build;
 }
 /**
  * Allows manipulation of the response object when performing a redirect.
  *
  * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  *   The Event to process.
  */
 public function checkRedirectUrl(FilterResponseEvent $event)
 {
     $response = $event->getResponse();
     if ($response instanceof RedirectResponse) {
         $options = array();
         $request = $event->getRequest();
         $destination = $request->query->get('destination');
         // A destination from \Drupal::request()->query always overrides the
         // current RedirectResponse. We do not allow absolute URLs to be passed
         // via \Drupal::request()->query, as this can be an attack vector, with
         // the following exception:
         // - Absolute URLs that point to this site (i.e. same base URL and
         //   base path) are allowed.
         if ($destination) {
             if (!UrlHelper::isExternal($destination)) {
                 // The destination query parameter can be a relative URL in the sense
                 // of not including the scheme and host, but its path is expected to
                 // be absolute (start with a '/'). For such a case, prepend the
                 // scheme and host, because the 'Location' header must be absolute.
                 if (strpos($destination, '/') === 0) {
                     $destination = $request->getSchemeAndHttpHost() . $destination;
                 } else {
                     // Legacy destination query parameters can be relative paths that
                     // have not yet been converted to URLs (outbound path processors
                     // and other URL handling still needs to be performed).
                     // @todo As generateFromPath() is deprecated, remove this in
                     //   https://www.drupal.org/node/2418219.
                     $destination = UrlHelper::parse($destination);
                     $path = $destination['path'];
                     $options['query'] = $destination['query'];
                     $options['fragment'] = $destination['fragment'];
                     // The 'Location' HTTP header must always be absolute.
                     $options['absolute'] = TRUE;
                     $destination = $this->urlGenerator->generateFromPath($path, $options);
                 }
                 $response->setTargetUrl($destination);
             } elseif (UrlHelper::externalIsLocal($destination, $this->requestContext->getCompleteBaseUrl())) {
                 $response->setTargetUrl($destination);
             }
         }
     }
 }
 /**
  * Allows manipulation of the response object when performing a redirect.
  *
  * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  *   The Event to process.
  */
 public function checkRedirectUrl(FilterResponseEvent $event)
 {
     $response = $event->getResponse();
     if ($response instanceof RedirectResponse) {
         $options = array();
         $destination = $event->getRequest()->query->get('destination');
         // A destination from \Drupal::request()->query always overrides the
         // current RedirectResponse. We do not allow absolute URLs to be passed
         // via \Drupal::request()->query, as this can be an attack vector, with
         // the following exception:
         // - Absolute URLs that point to this site (i.e. same base URL and
         //   base path) are allowed.
         if ($destination && (!UrlHelper::isExternal($destination) || UrlHelper::externalIsLocal($destination, $GLOBALS['base_url']))) {
             $destination = UrlHelper::parse($destination);
             $path = $destination['path'];
             $options['query'] = $destination['query'];
             $options['fragment'] = $destination['fragment'];
             // The 'Location' HTTP header must always be absolute.
             $options['absolute'] = TRUE;
             $response->setTargetUrl($this->urlGenerator->generateFromPath($path, $options));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $langcode = $this->entity->id();
     // Warn and redirect user when attempting to delete the default language.
     if (language_default()->id == $langcode) {
         drupal_set_message($this->t('The default language cannot be deleted.'));
         $url = $this->urlGenerator->generateFromPath('admin/config/regional/language', array('absolute' => TRUE));
         return new RedirectResponse($url);
     }
     // Throw a 404 when attempting to delete a non-existing language.
     $languages = language_list();
     if (!isset($languages[$langcode])) {
         throw new NotFoundHttpException();
     }
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function redirectForm(FormStateInterface $form_state)
 {
     // According to RFC 7231, 303 See Other status code must be used to redirect
     // user agent (and not default 302 Found).
     // @see http://tools.ietf.org/html/rfc7231#section-6.4.4
     $status_code = Response::HTTP_SEE_OTHER;
     $redirect = $form_state->getRedirect();
     // Allow using redirect responses directly if needed.
     if ($redirect instanceof RedirectResponse) {
         return $redirect;
     }
     $url = NULL;
     // Check for a route-based redirection.
     if ($redirect instanceof Url) {
         $url = $redirect->toString();
     } elseif (is_array($redirect)) {
         if (isset($redirect[1])) {
             $options = $redirect[1];
         } else {
             $options = array();
         }
         // Redirections should always use absolute URLs.
         $options['absolute'] = TRUE;
         if (isset($redirect[2])) {
             $status_code = $redirect[2];
         }
         $url = $this->urlGenerator->generateFromPath($redirect[0], $options);
     } elseif (is_string($redirect)) {
         // This function can be called from the installer, which guarantees
         // that $redirect will always be a string, so catch that case here
         // and use the appropriate redirect function.
         if ($this->drupalInstallationAttempted()) {
             install_goto($redirect);
         } else {
             $url = $this->urlGenerator->generateFromPath($redirect, array('absolute' => TRUE));
         }
     } elseif ($redirect === NULL) {
         $request = $this->requestStack->getCurrentRequest();
         // @todo Remove dependency on the internal _system_path attribute:
         //   https://www.drupal.org/node/2293521.
         $url = $this->urlGenerator->generateFromPath($request->attributes->get('_system_path'), array('query' => $request->query->all(), 'absolute' => TRUE));
     }
     if ($url) {
         return new RedirectResponse($url, $status_code);
     }
 }
Exemple #11
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, ViewExecutable $view = NULL, $output = NULL)
 {
     $form_state['step'] = isset($form_state['step']) ? $form_state['step'] : 'views_form_views_form';
     $form_state['step_controller']['views_form_views_form'] = 'Drupal\\views\\Form\\ViewsFormMainForm';
     // Cache the built form to prevent it from being rebuilt prior to validation
     // and submission, which could lead to data being processed incorrectly,
     // because the views rows (and thus, the form elements as well) have changed
     // in the meantime.
     $form_state['cache'] = TRUE;
     $form = array();
     $query = $this->requestStack->getCurrentRequest()->query->all();
     $query = UrlHelper::filterQueryParameters($query, array(), '');
     $form['#action'] = $this->urlGenerator->generateFromPath($view->getUrl(), array('query' => $query));
     // Tell the preprocessor whether it should hide the header, footer, pager...
     $form['show_view_elements'] = array('#type' => 'value', '#value' => $form_state['step'] == 'views_form_views_form' ? TRUE : FALSE);
     $form_object = $this->getFormObject($form_state);
     $form += $form_object->buildForm($form, $form_state, $view, $output);
     return $form;
 }
 /**
  * Converts the passed in destination into an absolute URL.
  *
  * @param string $destination
  *   The path for the destination. In case it starts with a slash it should
  *   have the base path included already.
  * @param string $scheme_and_host
  *   The scheme and host string of the current request.
  *
  * @return string
  *   The destination as absolute URL.
  */
 protected function getDestinationAsAbsoluteUrl($destination, $scheme_and_host)
 {
     if (!UrlHelper::isExternal($destination)) {
         // The destination query parameter can be a relative URL in the sense of
         // not including the scheme and host, but its path is expected to be
         // absolute (start with a '/'). For such a case, prepend the scheme and
         // host, because the 'Location' header must be absolute.
         if (strpos($destination, '/') === 0) {
             $destination = $scheme_and_host . $destination;
         } else {
             // Legacy destination query parameters can be relative paths that have
             // not yet been converted to URLs (outbound path processors and other
             // URL handling still needs to be performed).
             // @todo As generateFromPath() is deprecated, remove this in
             //   https://www.drupal.org/node/2418219.
             $destination = UrlHelper::parse($destination);
             $path = $destination['path'];
             $options = ['query' => $destination['query'], 'fragment' => $destination['fragment'], 'absolute' => TRUE];
             $destination = $this->urlGenerator->generateFromPath($path, $options);
         }
     }
     return $destination;
 }
 /**
  * {@inheritdoc}
  */
 public function redirectForm(FormStateInterface $form_state)
 {
     $redirect = $form_state->getRedirect();
     // Allow using redirect responses directly if needed.
     if ($redirect instanceof RedirectResponse) {
         return $redirect;
     }
     $url = NULL;
     // Check for a route-based redirection.
     if ($redirect instanceof Url) {
         $url = $redirect->setAbsolute()->toString();
     } elseif ($redirect === NULL) {
         $request = $this->requestStack->getCurrentRequest();
         // @todo Remove dependency on the internal _system_path attribute:
         //   https://www.drupal.org/node/2293521.
         $url = $this->urlGenerator->generateFromPath($request->attributes->get('_system_path'), array('query' => $request->query->all(), 'absolute' => TRUE));
     }
     if ($url) {
         // According to RFC 7231, 303 See Other status code must be used to redirect
         // user agent (and not default 302 Found).
         // @see http://tools.ietf.org/html/rfc7231#section-6.4.4
         return new RedirectResponse($url, Response::HTTP_SEE_OTHER);
     }
 }
Exemple #14
0
 /**
  * {@inheritdoc}
  */
 public function render()
 {
     $build = parent::render();
     $build['#empty'] = t('No content types available. <a href="@link">Add content type</a>.', array('@link' => $this->urlGenerator->generateFromPath('admin/structure/types/add')));
     return $build;
 }
 /**
  * Generates an absolute URL given a path.
  *
  * @param string $path
  *   The path.
  * @param array $options
  *   (optional) An associative array of additional options. The 'absolute'
  *   option is forced to be TRUE.
  *
  * @return string
  *   The generated absolute URL for the given path.
  */
 public function getUrlFromPath($path, $options = array())
 {
     $options['absolute'] = TRUE;
     return $this->urlGenerator->generateFromPath($path, $options);
 }
 /**
  * {@inheritdoc}
  */
 public function render()
 {
     $build = parent::render();
     $build['#empty'] = $this->t('There are currently no styles. <a href="!url">Add a new one</a>.', array('!url' => $this->urlGenerator->generateFromPath('admin/config/media/image-styles/add')));
     return $build;
 }
Exemple #17
0
 /**
  * {@inheritdoc}
  */
 public function redirectForm($form_state)
 {
     // Skip redirection for form submissions invoked via
     // \Drupal\Core\Form\FormBuilderInterface::submitForm().
     if (!empty($form_state['programmed'])) {
         return;
     }
     // Skip redirection if rebuild is activated.
     if (!empty($form_state['rebuild'])) {
         return;
     }
     // Skip redirection if it was explicitly disallowed.
     if (!empty($form_state['no_redirect'])) {
         return;
     }
     // Allow using redirect responses directly if needed.
     if (isset($form_state['redirect']) && $form_state['redirect'] instanceof RedirectResponse) {
         return $form_state['redirect'];
     }
     // Check for a route-based redirection.
     if (isset($form_state['redirect_route'])) {
         // @todo Remove once all redirects are converted to Url.
         if (!$form_state['redirect_route'] instanceof Url) {
             $form_state['redirect_route'] += array('route_parameters' => array(), 'options' => array());
             $form_state['redirect_route'] = new Url($form_state['redirect_route']['route_name'], $form_state['redirect_route']['route_parameters'], $form_state['redirect_route']['options']);
         }
         $form_state['redirect_route']->setAbsolute();
         // According to RFC 7231, 303 See Other status code must be used
         // to redirect user agent (and not default 302 Found).
         // @see http://tools.ietf.org/html/rfc7231#section-6.4.4
         return new RedirectResponse($form_state['redirect_route']->toString(), Response::HTTP_SEE_OTHER);
     }
     // Only invoke a redirection if redirect value was not set to FALSE.
     if (!isset($form_state['redirect']) || $form_state['redirect'] !== FALSE) {
         if (isset($form_state['redirect'])) {
             if (is_array($form_state['redirect'])) {
                 if (isset($form_state['redirect'][1])) {
                     $options = $form_state['redirect'][1];
                 } else {
                     $options = array();
                 }
                 // Redirections should always use absolute URLs.
                 $options['absolute'] = TRUE;
                 if (isset($form_state['redirect'][2])) {
                     $status_code = $form_state['redirect'][2];
                 } else {
                     $status_code = Response::HTTP_SEE_OTHER;
                 }
                 return new RedirectResponse($this->urlGenerator->generateFromPath($form_state['redirect'][0], $options), $status_code);
             } else {
                 // This function can be called from the installer, which guarantees
                 // that $redirect will always be a string, so catch that case here
                 // and use the appropriate redirect function.
                 if ($this->drupalInstallationAttempted()) {
                     install_goto($form_state['redirect']);
                 } else {
                     return new RedirectResponse($this->urlGenerator->generateFromPath($form_state['redirect'], array('absolute' => TRUE)), Response::HTTP_SEE_OTHER);
                 }
             }
         }
         $request = $this->requestStack->getCurrentRequest();
         // @todo Remove dependency on the internal _system_path attribute:
         //   https://www.drupal.org/node/2293521.
         $url = $this->urlGenerator->generateFromPath($request->attributes->get('_system_path'), array('query' => $request->query->all(), 'absolute' => TRUE));
         return new RedirectResponse($url, Response::HTTP_SEE_OTHER);
     }
 }