I am trying to set the node status as unpublished using the below code in ResourseBase custom Rest API but no luck.
Added the source code in below
namespace Drupal\custom_module_nam\Plugin\rest\resource;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @RestResource(
* id = "unpublish_node",
* label = @Translation("Unpublish node using webservice"),
* uri_paths = {
* "create" = "/services/rest/unpublish"
* }
* )
*/
class UnPublish extends ResourceBase {
protected $entityTypeManager;
protected $entity;
protected $entityStorage;
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->entityStorage = $entityTypeManager->getStorage('node');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
public function post($value) {
$entityArray = $this->entityStorage->loadByProperties(['field_name' => $value]);
$this->entity = reset($entityArray);
$this->entity->set('status', 0);
$this->entity->save();
}
}
Drupal Version is 9.5.1 and I need to unpublish the node in REST api call.
I erroneously updated the revision status as true in another function, but I have corrected it to false.