Chris@0: plugin_id) && isset($this->id)) { Chris@0: // Generate plugin_id on first entity creation. Chris@0: $this->plugin_id = str_replace('.', ':', $this->id); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * The label callback for this configuration entity. Chris@0: * Chris@0: * @return string The label. Chris@0: */ Chris@0: protected function getLabelFromPlugin() { Chris@0: $plugin_definition = $this->getResourcePluginManager() Chris@0: ->getDefinition(['id' => $this->plugin_id]); Chris@0: return $plugin_definition['label']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the resource plugin manager. Chris@0: * Chris@0: * @return \Drupal\Component\Plugin\PluginManagerInterface Chris@0: */ Chris@0: protected function getResourcePluginManager() { Chris@0: if (!isset($this->pluginManager)) { Chris@0: $this->pluginManager = \Drupal::service('plugin.manager.rest'); Chris@0: } Chris@0: return $this->pluginManager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getResourcePlugin() { Chris@0: return $this->getPluginCollections()['resource']->get($this->plugin_id); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMethods() { Chris@0: switch ($this->granularity) { Chris@0: case RestResourceConfigInterface::METHOD_GRANULARITY: Chris@0: return $this->getMethodsForMethodGranularity(); Chris@0: case RestResourceConfigInterface::RESOURCE_GRANULARITY: Chris@0: return $this->configuration['methods']; Chris@0: default: Chris@0: throw new \InvalidArgumentException('Invalid granularity specified.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves a list of supported HTTP methods for this resource. Chris@0: * Chris@0: * @return string[] Chris@0: * A list of supported HTTP methods. Chris@0: */ Chris@0: protected function getMethodsForMethodGranularity() { Chris@0: $methods = array_keys($this->configuration); Chris@0: return array_map([$this, 'normalizeRestMethod'], $methods); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getAuthenticationProviders($method) { Chris@0: switch ($this->granularity) { Chris@0: case RestResourceConfigInterface::METHOD_GRANULARITY: Chris@0: return $this->getAuthenticationProvidersForMethodGranularity($method); Chris@0: case RestResourceConfigInterface::RESOURCE_GRANULARITY: Chris@0: return $this->configuration['authentication']; Chris@0: default: Chris@0: throw new \InvalidArgumentException('Invalid granularity specified.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves a list of supported authentication providers. Chris@0: * Chris@0: * @param string $method Chris@0: * The request method e.g GET or POST. Chris@0: * Chris@0: * @return string[] Chris@0: * A list of supported authentication provider IDs. Chris@0: */ Chris@0: public function getAuthenticationProvidersForMethodGranularity($method) { Chris@0: $method = $this->normalizeRestMethod($method); Chris@0: if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_auth'])) { Chris@0: return $this->configuration[$method]['supported_auth']; Chris@0: } Chris@0: return []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormats($method) { Chris@0: switch ($this->granularity) { Chris@0: case RestResourceConfigInterface::METHOD_GRANULARITY: Chris@0: return $this->getFormatsForMethodGranularity($method); Chris@0: case RestResourceConfigInterface::RESOURCE_GRANULARITY: Chris@0: return $this->configuration['formats']; Chris@0: default: Chris@0: throw new \InvalidArgumentException('Invalid granularity specified.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves a list of supported response formats. Chris@0: * Chris@0: * @param string $method Chris@0: * The request method e.g GET or POST. Chris@0: * Chris@0: * @return string[] Chris@0: * A list of supported format IDs. Chris@0: */ Chris@0: protected function getFormatsForMethodGranularity($method) { Chris@0: $method = $this->normalizeRestMethod($method); Chris@0: if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_formats'])) { Chris@0: return $this->configuration[$method]['supported_formats']; Chris@0: } Chris@0: return []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPluginCollections() { Chris@0: return [ Chris@17: 'resource' => new DefaultSingleLazyPluginCollection($this->getResourcePluginManager(), $this->plugin_id, []), Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * (@inheritdoc) Chris@0: */ Chris@0: public function calculateDependencies() { Chris@0: parent::calculateDependencies(); Chris@0: Chris@0: foreach ($this->getRestResourceDependencies()->calculateDependencies($this) as $type => $dependencies) { Chris@0: foreach ($dependencies as $dependency) { Chris@0: $this->addDependency($type, $dependency); Chris@0: } Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function onDependencyRemoval(array $dependencies) { Chris@0: $parent = parent::onDependencyRemoval($dependencies); Chris@0: Chris@0: // If the dependency problems are not marked as fixed at this point they Chris@0: // should be related to the resource plugin and the config entity should Chris@0: // be deleted. Chris@0: $changed = $this->getRestResourceDependencies()->onDependencyRemoval($this, $dependencies); Chris@0: return $parent || $changed; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the REST resource dependencies. Chris@0: * Chris@0: * @return \Drupal\rest\Entity\ConfigDependencies Chris@0: */ Chris@0: protected function getRestResourceDependencies() { Chris@0: return \Drupal::service('class_resolver')->getInstanceFromDefinition(ConfigDependencies::class); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Normalizes the method. Chris@0: * Chris@0: * @param string $method Chris@0: * The request method. Chris@0: * Chris@0: * @return string Chris@0: * The normalized request method. Chris@0: */ Chris@0: protected function normalizeRestMethod($method) { Chris@0: return strtoupper($method); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function postSave(EntityStorageInterface $storage, $update = TRUE) { Chris@0: parent::postSave($storage, $update); Chris@0: Chris@0: \Drupal::service('router.builder')->setRebuildNeeded(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function postDelete(EntityStorageInterface $storage, array $entities) { Chris@0: parent::postDelete($storage, $entities); Chris@0: Chris@0: \Drupal::service('router.builder')->setRebuildNeeded(); Chris@0: } Chris@0: Chris@0: }