VarietyMultiFiles.php 2.09 KB
Newer Older
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
<?php

namespace Drupal\migrate_example_advanced_setup\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

/**
 * Provides varieties as two endpoints, one for reds and one for whites.
 *
 * @RestResource(
 *   id = "migrate_example_advanced_variety_multiple",
 *   label = @Translation("Advanced migration example - Variety data"),
 *   uri_paths = {
 *     "canonical" = "/migrate_example_advanced_variety_multiple/{type}"
 *   }
 * )
 */
class VarietyMultiFiles extends ResourceBase {

  /**
   * Responds to GET requests.
   *
   * @param string $type
   *   'red', 'white', or NULL to return all varieties.
   *
   * @return \Drupal\rest\ResourceResponse
   *   The response containing the requested variety data.
   */
  public function get($type = NULL) {
    $data = [];
    if (strtolower($type) != 'white') {
      $data['variety'][] = [
        'name' => 'Amarone',
35 36
        // The categoryid for 'red'.
        'parent' => 3,
37
        'details' => 'Italian Venoto region',
38 39 40 41
        'attributes' => [
          'rich',
          'aromatic',
        ],
42 43 44
      ];
      $data['variety'][] = [
        'name' => 'Barbaresco',
45 46
        // The categoryid for 'red'.
        'parent' => 3,
47
        'details' => 'Italian Piedmont region',
48 49 50 51
        'attributes' => [
          'smoky',
          'earthy',
        ],
52 53 54 55 56
      ];
    }
    if (strtolower($type) != 'red') {
      $data['variety'][] = [
        'name' => 'Kir',
57 58
        // The categoryid for 'white'.
        'parent' => 1,
59
        'details' => 'French Burgundy region',
60
        'attributes' => [],
61 62 63
      ];
      $data['variety'][] = [
        'name' => 'Pinot Grigio',
64 65
        // The categoryid for 'white'.
        'parent' => 1,
66
        'details' => 'From the northeast of Italy',
67 68 69 70 71
        'attributes' => [
          'fruity',
          'medium-bodied',
          'slightly sweet',
        ],
72 73 74 75 76 77 78
      ];
    }

    $response = new ResourceResponse($data, 200);
    return $response;
  }

79 80 81 82 83 84 85 86
  /**
   * {@inheritdoc}
   */
  public function permissions() {
    // Remove permissions so the resource is available to all.
    return [];
  }

87
}