CreateMateriau.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Drupal\materio_graphql\Plugin\GraphQL\DataProducer;
  3. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  4. use Drupal\Core\Session\AccountInterface;
  5. use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
  6. use Drupal\materio_graphql\GraphQL\Response\MateriauResponse;
  7. use Drupal\node\Entity\Node;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Creates a new materiau entity.
  11. *
  12. * @DataProducer(
  13. * id = "create_materiau",
  14. * name = @Translation("Create Materiau"),
  15. * description = @Translation("Creates a new materiau."),
  16. * produces = @ContextDefinition("any",
  17. * label = @Translation("Materiau")
  18. * ),
  19. * consumes = {
  20. * "data" = @ContextDefinition("any",
  21. * label = @Translation("Materiau data")
  22. * )
  23. * }
  24. * )
  25. */
  26. class CreateMateriau extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
  27. /**
  28. * The current user.
  29. *
  30. * @var \Drupal\Core\Session\AccountInterface
  31. */
  32. protected $currentUser;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  37. return new static(
  38. $configuration,
  39. $plugin_id,
  40. $plugin_definition,
  41. $container->get('current_user')
  42. );
  43. }
  44. /**
  45. * CreateMateriau constructor.
  46. *
  47. * @param array $configuration
  48. * A configuration array containing information about the plugin instance.
  49. * @param string $plugin_id
  50. * The plugin_id for the plugin instance.
  51. * @param array $plugin_definition
  52. * The plugin implementation definition.
  53. * @param \Drupal\Core\Session\AccountInterface $current_user
  54. * The current user.
  55. */
  56. public function __construct(array $configuration, string $plugin_id, array $plugin_definition, AccountInterface $current_user) {
  57. parent::__construct($configuration, $plugin_id, $plugin_definition);
  58. $this->currentUser = $current_user;
  59. }
  60. /**
  61. * Creates an materiau.
  62. *
  63. * @param array $data
  64. * The submitted values for the materiau.
  65. *
  66. * @return \Drupal\graphql_composable\GraphQL\Response\MateriauResponse
  67. * The newly created materiau.
  68. *
  69. * @throws \Exception
  70. */
  71. public function resolve(array $data) {
  72. $response = new MateriauResponse();
  73. if ($this->currentUser->hasPermission("create materiau content")) {
  74. $values = [
  75. 'type' => 'materiau',
  76. 'title' => $data['title'],
  77. 'body' => $data['description'],
  78. ];
  79. $node = Node::create($values);
  80. $node->save();
  81. $response->setMateriau($node);
  82. }
  83. else {
  84. $response->addViolation(
  85. $this->t('You do not have permissions to create materiaus.')
  86. );
  87. }
  88. return $response;
  89. }
  90. }