outline-8.x-1.x-dev/modules/outline_graphql/src/Plugin/GraphQL/DataProducer/Entry/Mutation/SetParent.php
modules/outline_graphql/src/Plugin/GraphQL/DataProducer/Entry/Mutation/SetParent.php
<?php
namespace Drupal\outline_graphql\Plugin\GraphQL\DataProducer\Entry\Mutation;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @DataProducer(
* id = "set_parent_entry",
* name = @Translation("Set Parent Entry"),
* description = @Translation("Sets the parent entry."),
* produces = @ContextDefinition("integer",
* label = @Translation("Original parent entry id")
* ),
* consumes = {
* "eid" = @ContextDefinition("integer",
* label = @Translation("Entry ID"),
* required = FALSE
* ),
* "parentEid" = @ContextDefinition("integer",
* label = @Translation("Parent Entry ID"),
* required = FALSE
* )
* }
* )
*/
class SetParent extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_user')
);
}
/**
* SetParent constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(array $configuration, string $plugin_id, array $plugin_definition, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
}
/**
* Sets the parent entry.
*
* @param array $data
* The title of the job.
*
* @return integer
* The parent entry ID of the entry.
*
* @throws \Exception
*/
public function resolve(int $eid, int $parentEid) {
//if ($this->currentUser->hasPermission("change parent entry")) {
/* @var $entry \Drupal\outline\EntryInterface */
// Get the entry.
$entry = \Drupal::entityTypeManager()
->getStorage('outline_entry')
->load($eid);
// Update Parent ID
/* @var $originalParent \Drupal\outline\EntryInterface */
$originalParentID = 0;
if ($parentEid == 0) {
$entry->set('parent', ['target_id' => 0]);
}
else {
// Set and save new parent ID.
$originalParentID =$entry->getParentEid();
$entry->set('parent', ['target_id' => $parentEid]);
}
$entry->save();
// Return parent entry eid.
return $originalParentID;
//}
//return NULL;
}
}
