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

modules/outline_graphql/src/Plugin/GraphQL/DataProducer/Entry/Mutation/Rename.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 = "rename_entry",
 *   name = @Translation("Rename Entry"),
 *   description = @Translation("Rename an entry."),
 *   produces = @ContextDefinition("boolean",
 *     label = @Translation("Success")
 *   ),
 *   consumes = {
 *     "eid" = @ContextDefinition("integer",
 *       label = @Translation("Entry ID"),
 *       required = TRUE
 *     ),
 *     "newName" = @ContextDefinition("string",
 *       label = @Translation("New name"),
 *       required = TRUE
 *     ),
 *   }
 * )
 */
class Rename 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')
    );
  }

  /**
   * Rename 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;
  }

  /**
   * Rename entry.
   *
   * @param int $eid
   *
   * @param string $newName
   *
   * @return bool
   *   True if entry was deleted.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException|\Drupal\Core\Entity\EntityStorageException
   */
  public function resolve(int $eid, string $newName) {
    //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
    $entry->setName($newName);
    $entry->save();
    // Return true if success.
    return TRUE;

    //}
    //return NULL;
  }

}

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

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