outline-8.x-1.x-dev/modules/outline_graphql/src/Plugin/GraphQL/DataProducer/Entry/Mutation/Add.php

modules/outline_graphql/src/Plugin/GraphQL/DataProducer/Entry/Mutation/Add.php
<?php

namespace Drupal\outline_graphql\Plugin\GraphQL\DataProducer\Entry\Mutation;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * @DataProducer(
 *   id = "add_entry",
 *   name = @Translation("Adds an entry to a specified parent entry."),
 *   description = @Translation("Adds a new child entry to a specified parent entry."),
 *   produces = @ContextDefinition("integer",
 *     label = @Translation("New child entry id")
 *   ),
 *   consumes = {
 *     "parentEid" = @ContextDefinition("integer",
 *       label = @Translation("Parent entry id"),
 *       required = FALSE
 *     ),
 *   }
 * )
 */
class Add extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $container->get('entity_type.manager'),
      $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.SetParent.php
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, array $configuration, string $plugin_id, array $plugin_definition, AccountInterface $current_user) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->currentUser = $current_user;
  }

  /**
   * Add child entry.
   *
   * @param integer $parentEid
   *
   * @return integer
   *   The new child's entry id.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function resolve(int $parentEid) {
    //if ($this->currentUser->hasPermission("create new entries")) {

    /* @var $parentEntry \Drupal\outline\EntryInterface */

    // Get the parent entry.
    $storage = $this->entityTypeManager->getStorage('outline_entry');
    $parentEntry = $storage->load($parentEid);

    // Get the outline id.
    $oid = $parentEntry->getOutlineId();

    // Create new child entry.
    $entry = $storage->create([
      'oid' => $oid,
      'parent' => $parentEid,
      'name' => 'New entry',
    ]);

    $entry->save();

    // Return new child's entry eid.
    return $entry->id();

    //}
    //return NULL;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc