summaryrefslogtreecommitdiffstats
path: root/core/modules/rest/rest.module
blob: ba61a17feabdd3950a42a0615bdef93b1d6276c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

/**
 * @file
 * RESTful web services module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\ViewEntityInterface;

/**
 * Implements hook_help().
 */
function rest_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.rest':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The RESTful Web Services module provides a framework for exposing REST resources on your site. It provides support for content entity types such as the main site content, comments, custom blocks, taxonomy terms, and user accounts, etc. (see the <a href=":field">Field module help page</a> for more information about entities). REST support for content items of the Node module is enabled by default, and support for other types of content entities can be enabled. Other modules may add support for other types of REST resources. For more information, see the <a href=":rest">online documentation for the RESTful Web Services module</a>.', [':rest' => 'https://www.drupal.org/documentation/modules/rest', ':field' => (\Drupal::moduleHandler()->moduleExists('field')) ? \Drupal::url('help.page', ['name' => 'field']) : '#']) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Installing supporting modules') . '</dt>';
      $output .= '<dd>' . t('In order to use REST on a web site, you need to install modules that provide serialization and authentication services. You can use the Core module <a href=":hal">HAL</a> for serialization and <a href=":basic_auth">HTTP Basic Authentication</a> for authentication, or install a contributed or custom module.', [':hal' => (\Drupal::moduleHandler()->moduleExists('hal')) ? \Drupal::url('help.page', ['name' => 'hal']) : '#', ':basic_auth' => (\Drupal::moduleHandler()->moduleExists('basic_auth')) ? \Drupal::url('help.page', ['name' => 'basic_auth']) : '#']) . '</dd>';
      $output .= '<dt>' . t('Enabling REST support for an entity type') . '</dt>';
      $output .= '<dd>' . t('REST support for content types (provided by the <a href=":node">Node</a> module) is enabled by default. To enable support for other content entity types, you can use a <a href=":config" target="blank">process based on configuration editing</a> or the contributed <a href=":restui">REST UI module</a>.', [':node' => (\Drupal::moduleHandler()->moduleExists('node')) ? \Drupal::url('help.page', ['name' => 'node']) : '#', ':config' => 'https://www.drupal.org/documentation/modules/rest', ':restui' => 'https://www.drupal.org/project/restui']) . '</dd>';
      $output .= '<dd>' . t('You will also need to grant anonymous users permission to perform each of the REST operations you want to be available, and set up authentication properly to authorize web requests.') . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_view_presave().
 *
 * @see rest_update_8401()
 */
function rest_view_presave(ViewEntityInterface $view) {
  // Fix the auth options on import, much like what rest_update_8401() does.
  $auth_providers = \Drupal::service('authentication_collector')->getSortedProviders();
  $process_auth = function ($auth_option) use ($auth_providers) {
    foreach ($auth_providers as $provider_id => $provider_data) {
      // The provider belongs to the module that declares it as a service.
      if (strtok($provider_data->_serviceId, '.') === $auth_option) {
        return $provider_id;
      }
    }

    return $auth_option;
  };

  foreach (array_keys($view->get('display')) as $display_id) {
    $display = &$view->getDisplay($display_id);
    if ($display['display_plugin'] === 'rest_export' && !empty($display['display_options']['auth'])) {
      $display['display_options']['auth'] = array_map($process_auth, $display['display_options']['auth']);
    }
  }
}