Skip to main content
improved formatting
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Custom Migration of a custom Rest API using PATCH/POST migrated from Drupal 8 to Drupal 9

I had a custom restapiREST API working with Drupal 8 perfectly, after update. After upgrading to Drupal 9.2.X., the PatchPatch and PostPost methods of this custom rest api doesn't works anymore, gettingREST API stopped working. Now they throw the following error log:exception.

RuntimeException: Callable "Drupal\my_module\Plugin\rest\resource\UpdateBoxes::patch" requires a value for the "$payload" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 143 of /var/www/docroot/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).

RuntimeException: Callable "Drupal\my_module\Plugin\rest\resource\UpdateBoxes::patch" requires a value for the "$payload" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 143 of /var/www/docroot/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).

The code that i had for this restapi was:I am using is the following one.

namespace Drupal\my_module\Plugin\rest\resource;

use Drupal\rest\ModifiedResourceResponse;

/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "update_boxes",
 *   label = @Translation("Update boxes"),
 *   uri_paths = {
 *     "canonical" = "/api/container/{container_id}/{type}/{product_id}/boxes"
 *   }
 * )
 */
class UpdateBoxes extends ResourceBase {

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  protected $boxesHandler;

  protected $event_dispacher;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->currentUser = $container->get('current_user');
    $instance->boxesHandler = $container->get('boxes.handler');
    $instance->event_dispacher = $container->get('event_dispatcher');

    return $instance;
  }

  /**
   * Responds to PATCH requests.
   *
   * @param $container_id
   * @param $type
   * @param $product_id
   * @param $payload
   * @return \Drupal\rest\ModifiedResourceResponse
   *   The HTTP response object.
   */
    public function patch($container_id = null, $type = null, $product_id = null, $payload) {
      //My code here.
     
    }

}
namespace Drupal\my_module\Plugin\rest\resource;

use Drupal\rest\ModifiedResourceResponse;

/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "update_boxes",
 *   label = @Translation("Update boxes"),
 *   uri_paths = {
 *     "canonical" = "/api/container/{container_id}/{type}/{product_id}/boxes"
 *   }
 * )
 */
class UpdateBoxes extends ResourceBase {

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  protected $boxesHandler;

  protected $event_dispacher;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->currentUser = $container->get('current_user');
    $instance->boxesHandler = $container->get('boxes.handler');
    $instance->event_dispacher = $container->get('event_dispatcher');

    return $instance;
  }

  /**
   * Responds to PATCH requests.
   *
   * @param $container_id
   * @param $type
   * @param $product_id
   * @param $payload
   * @return \Drupal\rest\ModifiedResourceResponse
   *   The HTTP response object.
   */
    public function patch($container_id = null, $type = null, $product_id = null, $payload) {
      //My code here.
     
    }

}

And in the patch method iIn patch() I could read the variable $payload$payload and see the array sent in the patch request, but after runupgrading the Drupal 9 update $payloadsite, $payload is NULLNULL.

The solution that i appliedI found was changechanging the last argument "$payload"of patch() and use the request to extract the payload from the $request$request object.

public function patch($container_id = null, $type = null, $product_id = null, Request $request) {
          //My code here.
          $payload = json_decode($request->getContent(), TRUE);
         
        }
public function patch($container_id = null, $type = null, $product_id = null, Request $request) {
  // …
  $payload = json_decode($request->getContent(), TRUE);
}

But iI couldn't find any documentation about this change, and i don'tchange; I am not sure that it was a good practice or the better implementation to get the payload working with patch rests.

I would appreciate if you know something about it and if this solution helped you.

Custom Rest API using PATCH/POST migrated from Drupal 8 to Drupal 9

I had a custom restapi working with Drupal 8 perfectly, after update to Drupal 9.2.X. the Patch and Post methods of this custom rest api doesn't works anymore, getting the following error log:

RuntimeException: Callable "Drupal\my_module\Plugin\rest\resource\UpdateBoxes::patch" requires a value for the "$payload" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 143 of /var/www/docroot/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).

The code that i had for this restapi was:

namespace Drupal\my_module\Plugin\rest\resource;

use Drupal\rest\ModifiedResourceResponse;

/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "update_boxes",
 *   label = @Translation("Update boxes"),
 *   uri_paths = {
 *     "canonical" = "/api/container/{container_id}/{type}/{product_id}/boxes"
 *   }
 * )
 */
class UpdateBoxes extends ResourceBase {

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  protected $boxesHandler;

  protected $event_dispacher;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->currentUser = $container->get('current_user');
    $instance->boxesHandler = $container->get('boxes.handler');
    $instance->event_dispacher = $container->get('event_dispatcher');

    return $instance;
  }

  /**
   * Responds to PATCH requests.
   *
   * @param $container_id
   * @param $type
   * @param $product_id
   * @param $payload
   * @return \Drupal\rest\ModifiedResourceResponse
   *   The HTTP response object.
   */
    public function patch($container_id = null, $type = null, $product_id = null, $payload) {
      //My code here.
     
    }

}

And in the patch method i could read the variable $payload and see the array sent in the patch request, but after run the Drupal 9 update $payload is NULL.

The solution that i applied was change the last argument "$payload" and use the request to extract the payload from the $request object.

public function patch($container_id = null, $type = null, $product_id = null, Request $request) {
          //My code here.
          $payload = json_decode($request->getContent(), TRUE);
         
        }

But i couldn't find any documentation about this change, and i don't sure that it was a good practice or the better implementation to get the payload working with patch rests.

I would appreciate if you know something about it and if this solution helped you.

Migration of a custom Rest API using PATCH/POST

I had a custom REST API working with Drupal 8 perfectly. After upgrading to Drupal 9.2.X, the Patch and Post methods of this custom REST API stopped working. Now they throw the following exception.

RuntimeException: Callable "Drupal\my_module\Plugin\rest\resource\UpdateBoxes::patch" requires a value for the "$payload" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 143 of /var/www/docroot/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).

The code I am using is the following one.

namespace Drupal\my_module\Plugin\rest\resource;

use Drupal\rest\ModifiedResourceResponse;

/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "update_boxes",
 *   label = @Translation("Update boxes"),
 *   uri_paths = {
 *     "canonical" = "/api/container/{container_id}/{type}/{product_id}/boxes"
 *   }
 * )
 */
class UpdateBoxes extends ResourceBase {

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  protected $boxesHandler;

  protected $event_dispacher;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->currentUser = $container->get('current_user');
    $instance->boxesHandler = $container->get('boxes.handler');
    $instance->event_dispacher = $container->get('event_dispatcher');

    return $instance;
  }

  /**
   * Responds to PATCH requests.
   *
   * @param $container_id
   * @param $type
   * @param $product_id
   * @param $payload
   * @return \Drupal\rest\ModifiedResourceResponse
   *   The HTTP response object.
   */
    public function patch($container_id = null, $type = null, $product_id = null, $payload) {
      //My code here.
     
    }

}

In patch() I could read $payload and see the array sent in the request, but after upgrading the site, $payload is NULL.

The solution I found was changing the last argument of patch() and extract the payload from the $request object.

public function patch($container_id = null, $type = null, $product_id = null, Request $request) {
  // …
  $payload = json_decode($request->getContent(), TRUE);
}

I couldn't find any documentation about this change; I am not sure it was a good practice or the better implementation to get the payload working.

Source Link
Mauricio
  • 117
  • 1
  • 9

Custom Rest API using PATCH/POST migrated from Drupal 8 to Drupal 9

I had a custom restapi working with Drupal 8 perfectly, after update to Drupal 9.2.X. the Patch and Post methods of this custom rest api doesn't works anymore, getting the following error log:

RuntimeException: Callable "Drupal\my_module\Plugin\rest\resource\UpdateBoxes::patch" requires a value for the "$payload" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 143 of /var/www/docroot/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).

The code that i had for this restapi was:

namespace Drupal\my_module\Plugin\rest\resource;

use Drupal\rest\ModifiedResourceResponse;

/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "update_boxes",
 *   label = @Translation("Update boxes"),
 *   uri_paths = {
 *     "canonical" = "/api/container/{container_id}/{type}/{product_id}/boxes"
 *   }
 * )
 */
class UpdateBoxes extends ResourceBase {

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  protected $boxesHandler;

  protected $event_dispacher;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->currentUser = $container->get('current_user');
    $instance->boxesHandler = $container->get('boxes.handler');
    $instance->event_dispacher = $container->get('event_dispatcher');

    return $instance;
  }

  /**
   * Responds to PATCH requests.
   *
   * @param $container_id
   * @param $type
   * @param $product_id
   * @param $payload
   * @return \Drupal\rest\ModifiedResourceResponse
   *   The HTTP response object.
   */
    public function patch($container_id = null, $type = null, $product_id = null, $payload) {
      //My code here.
     
    }

}

And in the patch method i could read the variable $payload and see the array sent in the patch request, but after run the Drupal 9 update $payload is NULL.

The solution that i applied was change the last argument "$payload" and use the request to extract the payload from the $request object.

public function patch($container_id = null, $type = null, $product_id = null, Request $request) {
          //My code here.
          $payload = json_decode($request->getContent(), TRUE);
         
        }

But i couldn't find any documentation about this change, and i don't sure that it was a good practice or the better implementation to get the payload working with patch rests.

I would appreciate if you know something about it and if this solution helped you.