1

I'm working on creating my first custom REST resource in D8, and following this blog post, I have the following class in the /modules/custom/video_export/src/Plugin/rest/resource directory:

<?php
/**
 * @file
 * Custom Rest resource for video nodes.
 */

namespace Drupal\video_export\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Psr\Log\LoggerInterface;

/**
 * Provides a resource for database watchdog log entries.
 *
 * @RestResource(
 *   id = "video_export_source",
 *   label = @Translation("Video Export Source"),
 *   uri_paths = {
 *     "canonical" = "/videos/feed/{site}"
 *   }
 * )
 */

class VideoExportResource extends ResourceBase {
  /**
   *  A curent user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
    * @param array $configuration
    *   A configuration array containing information about the plugin instance.
    * @param string $plugin_id
    *   The plugin_id for the plugin instance.
    * @param mixed $plugin_definition
    *   The plugin implementation definition.
    * @param array $serializer_formats
    *   The available serialization formats.
    * @param \Psr\Log\LoggerInterface $logger
    *   A logger instance.
    * @param \Drupal\Core\Session\AccountProxyInterface $current_user
    *   A current user instance.
  */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    array $serializer_formats,
    LoggerInterface $logger,
    AccountProxyInterface $current_user) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);

    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->getParameter('serializer.formats'),
      $container->get('logger.factory')->get('rest'),
      $container->get('current_user')
    );
  }

  /**
    * Responds to GET requests.
    *
    * Returns a list of video nodes to be imported into the target site.
    *
    * @return \Drupal\rest\ResourceResponse
    *   The response containing a list of bundle names.
    *
    * @throws \Symfony\Component\HttpKernel\Exception\HttpException
 */
  public function get($site = NULL) {
    $response = ['message' => 'Hello, this is a rest service'];
    return new ResourceResponse($response);
  }
}

However, I'm having multiple problems trying to use this code:

First, my IDE (PHPStorm) is not recognizing the namespace keyword. In addition, in my the rest dir in that path is highlighted in red, and it shows an error message on the resource keyword that says

Undefined constant resource: declaration of referenced constant is not found in built-in library and project files.

I've verified that I have the RESTful Web Services module enabled.

Also, I have the REST UI module enabled, and it lists my resource, but when I try to enable it, it tells me undefined namespace Drupal\video_export\Plugin\rest\resource', which I'm sure is because of the other errors.

I've looked this over and over and can't see what's wrong

3
  • Sometimes for me in PHPStorm doesn't recognize namespace for the first type either. Copy it, delete the line, and paste it.
    – ssibal
    Commented Apr 11, 2017 at 12:51
  • @Jdrupal We don't need the rest tag. If it has been removed, there is a reason.
    – avpaderno
    Commented Jul 10, 2019 at 12:31
  • Ah, ok. Thanks for letting me know.
    – user72672
    Commented Jul 10, 2019 at 12:35

1 Answer 1

0

As indicated by the comment by @ssibal, I just had to remove the line and then manually add it back in. The second time it took fine, and my resource works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.