Last active
February 27, 2019 00:12
-
-
Save superbiche/8fd132987a62138110103f4aa91412f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/Plugin/rest/resource/RestMenuItemsResource.php b/src/Plugin/rest/resource/RestMenuItemsResource.php | |
index 5c0f764..049898e 100644 | |
--- a/src/Plugin/rest/resource/RestMenuItemsResource.php | |
+++ b/src/Plugin/rest/resource/RestMenuItemsResource.php | |
@@ -13,7 +13,8 @@ use Drupal\Core\Url; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
use Psr\Log\LoggerInterface; | |
- | |
+use Drupal\consumers\Negotiator; | |
+use \InvalidArgumentException; | |
/** | |
* Provides a resource to get bundles by entity. | |
* | |
@@ -62,6 +63,13 @@ class RestMenuItemsResource extends ResourceBase { | |
*/ | |
protected $minDepth = 1; | |
+ protected $siteSlug; | |
+ | |
+ /** | |
+ * @var \Drupal\consumers\Negotiator | |
+ */ | |
+ protected $consumerNegotiator; | |
+ | |
/** | |
* {@inheritdoc} | |
*/ | |
@@ -72,11 +80,13 @@ class RestMenuItemsResource extends ResourceBase { | |
array $serializer_formats, | |
LoggerInterface $logger, | |
AliasManagerInterface $alias_manager, | |
- ConfigFactoryInterface $config_factory) { | |
+ ConfigFactoryInterface $config_factory, | |
+ Negotiator $consumer_negociator) { | |
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); | |
$this->aliasManager = $alias_manager; | |
$this->configFactory = $config_factory; | |
+ $this->consumerNegotiator = $consumer_negociator; | |
} | |
/** | |
@@ -90,7 +100,8 @@ class RestMenuItemsResource extends ResourceBase { | |
$container->getParameter('serializer.formats'), | |
$container->get('logger.factory')->get('rest'), | |
$container->get('path.alias_manager'), | |
- $container->get('config.factory') | |
+ $container->get('config.factory'), | |
+ $container->get('consumer.negotiator') | |
); | |
} | |
@@ -180,6 +191,14 @@ class RestMenuItemsResource extends ResourceBase { | |
$config = $this->configFactory->get('rest_menu_items.config'); | |
$outputValues = $config->get('output_values'); | |
+ // Fetch the site data. | |
+ $consumer = $this->consumerNegotiator->negotiateFromRequest(\Drupal::request()); | |
+ $site = $consumer->vesta_site ? $consumer->vesta_site->entity : null; | |
+ if (!$site) { | |
+ throw new InvalidArgumentException('No value for vesta_site found for this Consumer or no Consumer uuid found in request. Make sure you include either X-Consumer-ID header or _consumer_id query param in your request, and make sure every Consumer has a "Site" Group entity referenced.'); | |
+ } | |
+ $this->siteSlug = $site->field_slug->getString(); | |
+ | |
// Loop through the menu items. | |
foreach ($tree as $item_value) { | |
/* @var $org_link \Drupal\Core\Menu\MenuLinkInterface */ | |
@@ -359,6 +378,10 @@ class RestMenuItemsResource extends ResourceBase { | |
break; | |
} | |
+ if (in_array($key, ['alias', 'absolute', 'relative']) && stristr($value, $this->siteSlug . '/')) { | |
+ $value = str_replace($this->siteSlug . '/', '', $value); | |
+ } | |
+ | |
$returnArray[$key] = $value; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment