VarietyItems.php 1.79 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
<?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_items",
 *   label = @Translation("Advanced migration example - Variety data"),
 *   uri_paths = {
 *     "canonical" = "/migrate_example_advanced_variety_list/{variety}"
 *   }
 * )
 */
class VarietyItems extends ResourceBase {

  /**
   * Responds to GET requests.
   *
   * @param string $variety
   *   Machine name of the variety to retrieve.
   *
   * @return \Drupal\rest\ResourceResponse
   *   The response containing the requested variety data.
   */
  public function get($variety = NULL) {
    $varieties = [
      'retsina' => [
        'name' => 'Retsina',
34 35
        // The categoryid for 'white'.
        'parent' => 1,
36 37 38 39
        'details' => 'Greek',
      ],
      'trebbiano' => [
        'name' => 'Trebbiano',
40 41
        // The categoryid for 'white'.
        'parent' => 1,
42 43 44 45
        'details' => 'Italian',
      ],
      'valpolicella' => [
        'name' => 'Valpolicella',
46 47
        // The categoryid for 'red'.
        'parent' => 3,
48 49 50 51
        'details' => 'Italian Venoto region',
      ],
      'bardolino' => [
        'name' => 'Bardolino',
52 53
        // The categoryid for 'red'.
        'parent' => 3,
54 55 56 57 58 59 60 61 62 63 64 65 66 67
        'details' => 'Italian Venoto region',
      ],
    ];
    if (isset($varieties[$variety])) {
      $data = ['variety' => $varieties[$variety]];
    }
    else {
      $data = [];
    }

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

68 69 70 71 72 73 74 75
  /**
   * {@inheritdoc}
   */
  public function permissions() {
    // Remove permissions so the resource is available to all.
    return [];
  }

76
}