You are here

class ServiceEndpointResources in Services 9.0.x

Same name and namespace in other branches
  1. 8.4 src/Controller/ServiceEndpointResources.php \Drupal\services\Controller\ServiceEndpointResources

Class \Drupal\services\Controller\ServiceEndpointManageResource.

Hierarchy

Expanded class hierarchy of ServiceEndpointResources

File

src/Controller/ServiceEndpointResources.php, line 15

Namespace

Drupal\services\Controller
View source
class ServiceEndpointResources extends ControllerBase {

  /**
   * Service definition plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface.
   */
  protected $pluginManager;

  /**
   * Constructor for \Drupal\services\Form\ServiceEndpointResourceForm.
   */
  public function __construct(PluginManagerInterface $plugin_manager) {
    $this->pluginManager = $plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.services.service_definition'));
  }

  /**
   * List service resources.
   *
   * @param \Drupal\services\ServiceEndpointInterface|null $service_endpoint
   *   A service endpoint entity.
   *
   * @return array
   *   An renderable array.
   */
  public function displayList(ServiceEndpointInterface $service_endpoint = NULL) {
    $build = [];
    foreach ($this
      ->getCategoryDefinitions($service_endpoint) as $category => $definitions) {
      if (!isset($category)) {
        continue;
      }
      $build[$category] = [
        '#type' => 'details',
        '#title' => $this
          ->t('@category', [
          '@category' => $category,
        ]),
        '#tree' => TRUE,
      ];
      $rows = [];
      foreach ($definitions as $plugin_id => $definition) {
        $row = $this
          ->buildRow($service_endpoint, $definition);
        $row['operations']['data'] = [
          '#type' => 'dropbutton',
          '#links' => $this
            ->buildOperationLinks($service_endpoint, $plugin_id),
        ];
        $rows[] = $row;
      }
      $build[$category]['table'] = array(
        '#type' => 'table',
        '#rows' => $rows,
        '#header' => $this
          ->buildHeader(),
        '#empty' => $this
          ->t('No service definitions exist'),
      );
    }
    return $build;
  }

  /**
   * Build service resource table header.
   *
   * @return array
   *   An array of table headers.
   */
  protected function buildHeader() {
    return [
      'title' => $this
        ->t('Definition'),
      'endpoint' => $this
        ->t('Endpoint'),
      'operations' => $this
        ->t('Operations'),
    ];
  }

  /**
   * Build service resource table rows.
   *
   * @return array
   *   An array of row items.
   */
  protected function buildRow(ServiceEndpointInterface $service_endpoint, array $definition) {
    $row = [];
    $row['title']['data'] = [
      '#markup' => $definition['title'],
    ];
    $row['endpoint']['data'] = [
      '#markup' => $definition['path'],
    ];
    return $row;
  }

  /**
   * Build service resource operations links.
   *
   * @param \Drupal\services\ServiceEndpointInterface $service_endpoint
   *   An service endpoint object.
   * @param string $plugin_id
   *   An service plugin identifier.
   *
   * @return array
   *   An array of operations links.
   */
  protected function buildOperationLinks(ServiceEndpointInterface $service_endpoint, $plugin_id) {
    $links = [];
    $links['configure'] = [
      'title' => $this
        ->t('Enable'),
      'url' => Url::fromRoute('entity.service_endpoint_resource.config_form', [
        'plugin_id' => $plugin_id,
        'service_endpoint' => $service_endpoint
          ->id(),
      ]),
      'attributes' => $this
        ->getModalAttributes(),
    ];
    if ($service_resource = $service_endpoint
      ->loadResourceProvider($plugin_id)) {
      if ($service_resource
        ->hasLinkTemplate('delete-form')) {
        $links['disable'] = [
          'title' => $this
            ->t('Disable'),
          'url' => $service_resource
            ->toUrl('delete-form'),
          'attributes' => $this
            ->getModalAttributes(),
        ];
      }
      $links['configure']['title'] = $this
        ->t('Configure');
    }
    return $links;
  }

  /**
   * Get service definitions grouped by category.
   *
   * @return array
   *   An array of resource definitions keyed by category.
   */
  protected function getCategoryDefinitions() {
    $definitions = [];
    foreach ($this->pluginManager
      ->getDefinitions() as $plugin_id => $definition) {
      if (!isset($definition['category'])) {
        continue;
      }
      $category = $definition['category']
        ->render();
      $definitions[$category][$plugin_id] = $definition;
    }
    ksort($definitions);
    return $definitions;
  }

  /**
   * Get AJAX modal attributes.
   *
   * @return array
   *   An array of modal attributes.
   */
  protected function getModalAttributes() {
    return [
      'class' => [
        'use-ajax',
      ],
      'data-dialog-type' => 'modal',
      'data-dialog-options' => Json::encode([
        'width' => 800,
      ]),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
ServiceEndpointResources::$pluginManager protected property Service definition plugin manager.
ServiceEndpointResources::buildHeader protected function Build service resource table header.
ServiceEndpointResources::buildOperationLinks protected function Build service resource operations links.
ServiceEndpointResources::buildRow protected function Build service resource table rows.
ServiceEndpointResources::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
ServiceEndpointResources::displayList public function List service resources.
ServiceEndpointResources::getCategoryDefinitions protected function Get service definitions grouped by category.
ServiceEndpointResources::getModalAttributes protected function Get AJAX modal attributes.
ServiceEndpointResources::__construct public function Constructor for \Drupal\services\Form\ServiceEndpointResourceForm.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.