Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
experience_builder
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
experience_builder
Merge requests
!571
Issue
#3500052
: Provide API to list available pages.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Issue
#3500052
: Provide API to list available pages.
issue/experience_builder-3500052:3500052-provide-an-api
into
0.x
Overview
127
Commits
25
Pipelines
28
Changes
8
Merged
Amandeep Singh
requested to merge
issue/experience_builder-3500052:3500052-provide-an-api
into
0.x
3 months ago
Overview
127
Commits
25
Pipelines
28
Changes
8
Expand
Closes
#3500052
0
0
Merge request reports
Compare
0.x
version 28
969ce1c8
3 months ago
version 27
969ce1c8
3 months ago
version 26
969ce1c8
3 months ago
version 25
5efc4275
3 months ago
version 24
d5488f7c
3 months ago
version 23
5ec7ae61
3 months ago
version 22
1911ca2c
3 months ago
version 21
42c5aa9a
3 months ago
version 20
7525a012
3 months ago
version 19
29326461
3 months ago
version 18
ea4c0f4f
3 months ago
version 17
61b5f681
3 months ago
version 16
a5d5c793
3 months ago
version 15
05128668
3 months ago
version 14
86d7b471
3 months ago
version 13
253af4d9
3 months ago
version 12
3c84f3f1
3 months ago
version 11
8c55a635
3 months ago
version 10
284c28a8
3 months ago
version 9
a6743525
3 months ago
version 8
b2f37c40
3 months ago
version 7
4b527dec
3 months ago
version 6
333b1ea1
3 months ago
version 5
444d216b
3 months ago
version 4
4bde959a
3 months ago
version 3
cb794dab
3 months ago
version 2
a586d94a
3 months ago
version 1
9d551e6c
3 months ago
0.x (base)
and
latest version
latest version
969ce1c8
25 commits,
3 months ago
version 28
969ce1c8
25 commits,
3 months ago
version 27
969ce1c8
25 commits,
3 months ago
version 26
969ce1c8
25 commits,
3 months ago
version 25
5efc4275
23 commits,
3 months ago
version 24
d5488f7c
22 commits,
3 months ago
version 23
5ec7ae61
21 commits,
3 months ago
version 22
1911ca2c
20 commits,
3 months ago
version 21
42c5aa9a
19 commits,
3 months ago
version 20
7525a012
18 commits,
3 months ago
version 19
29326461
17 commits,
3 months ago
version 18
ea4c0f4f
17 commits,
3 months ago
version 17
61b5f681
16 commits,
3 months ago
version 16
a5d5c793
15 commits,
3 months ago
version 15
05128668
14 commits,
3 months ago
version 14
86d7b471
13 commits,
3 months ago
version 13
253af4d9
12 commits,
3 months ago
version 12
3c84f3f1
11 commits,
3 months ago
version 11
8c55a635
11 commits,
3 months ago
version 10
284c28a8
10 commits,
3 months ago
version 9
a6743525
9 commits,
3 months ago
version 8
b2f37c40
8 commits,
3 months ago
version 7
4b527dec
7 commits,
3 months ago
version 6
333b1ea1
6 commits,
3 months ago
version 5
444d216b
5 commits,
3 months ago
version 4
4bde959a
4 commits,
3 months ago
version 3
cb794dab
3 commits,
3 months ago
version 2
a586d94a
2 commits,
3 months ago
version 1
9d551e6c
1 commit,
3 months ago
8 files
+
313
−
107
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
8
Search (e.g. *.vue) (Ctrl+P)
src/Controller/ApiContentControllers.php
0 → 100644
+
116
−
0
Options
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\experience_builder\Controller
;
use
Drupal\Core\Cache\CacheableJsonResponse
;
use
Drupal\Core\Cache\CacheableMetadata
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Entity\Query\QueryInterface
;
use
Drupal\Core\Render\RenderContext
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\Core\StringTranslation\StringTranslationTrait
;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\HttpFoundation\Response
;
/**
* HTTP API for interacting with XB-eligible Content entity types.
*
* @internal This HTTP API is intended only for the XB UI. These controllers
* and associated routes may change at any time.
*
* @todo https://www.drupal.org/i/3498525 should generalize this to all eligible content entity types
*/
final
class
ApiContentControllers
{
use
StringTranslationTrait
;
public
function
__construct
(
private
readonly
EntityTypeManagerInterface
$entityTypeManager
,
private
readonly
RendererInterface
$renderer
,
)
{}
public
function
post
():
JsonResponse
{
// Note: this intentionally does not catch content entity type storage
// handler exceptions: the generic XB API exception subscriber handles them.
// @see \Drupal\experience_builder\EventSubscriber\ApiExceptionSubscriber
$page
=
$this
->
entityTypeManager
->
getStorage
(
'xb_page'
)
->
create
([
'title'
=>
$this
->
t
(
'Untitled page'
),
'status'
=>
FALSE
,
]);
$page
->
save
();
return
new
JsonResponse
([
'entity_type'
=>
$page
->
getEntityTypeId
(),
'entity_id'
=>
$page
->
id
(),
],
RESPONSE
::
HTTP_CREATED
);
}
/**
* Returns a list of XB Page content entities, with only high-level metadata.
*
* TRICKY: there are reasons XB has its own internal HTTP API rather than
* using Drupal core's JSON:API. As soon as this method is updated to return
* all fields instead of just high-level metadata, those reasons may start to
* outweigh the downsides of adding a dependency on JSON:API.
*
* @see https://www.drupal.org/project/experience_builder/issues/3500052#comment-15966496
*/
public
function
list
():
CacheableJsonResponse
{
// @todo introduce pagination in https://www.drupal.org/i/3502691
$storage
=
$this
->
entityTypeManager
->
getStorage
(
'xb_page'
);
$query_cacheability
=
(
new
CacheableMetadata
())
->
addCacheContexts
(
$storage
->
getEntityType
()
->
getListCacheContexts
())
->
addCacheTags
(
$storage
->
getEntityType
()
->
getListCacheTags
());
$url_cacheability
=
new
CacheableMetadata
();
// We don't need to worry about the status of the page, as we need both
// published and unpublished pages on the frontend.
$entity_query
=
$storage
->
getQuery
()
->
accessCheck
(
TRUE
);
$ids
=
$this
->
executeQueryInRenderContext
(
$entity_query
,
$query_cacheability
);
/** @var \Drupal\Core\Entity\EntityPublishedInterface[] $content_entities */
$content_entities
=
$storage
->
loadMultiple
(
$ids
);
$content_list
=
[];
foreach
(
$content_entities
as
$content_entity
)
{
$id
=
(
int
)
$content_entity
->
id
();
$generated_url
=
$content_entity
->
toUrl
()
->
toString
(
TRUE
);
$content_list
[
$id
]
=
[
'id'
=>
$id
,
'title'
=>
$content_entity
->
label
(),
'status'
=>
$content_entity
->
isPublished
(),
'path'
=>
$generated_url
->
getGeneratedUrl
(),
];
$url_cacheability
->
addCacheableDependency
(
$generated_url
);
}
$json_response
=
new
CacheableJsonResponse
(
$content_list
);
// @todo add cache contexts for query params when introducing pagination in https://www.drupal.org/i/3502691.
$json_response
->
addCacheableDependency
(
$query_cacheability
)
->
addCacheableDependency
(
$url_cacheability
);
return
$json_response
;
}
/**
* Executes the query in a render context, to catch bubbled cacheability.
*
* @param \Drupal\Core\Entity\Query\QueryInterface $query
* The query to execute to get the return results.
* @param \Drupal\Core\Cache\CacheableMetadata $query_cacheability
* The value object to carry the query cacheability.
*
* @return array
* Returns IDs of entities.
*
* @see \Drupal\jsonapi\Controller\EntityResource::executeQueryInRenderContext()
*/
private
function
executeQueryInRenderContext
(
QueryInterface
$query
,
CacheableMetadata
$query_cacheability
)
:
array
{
$context
=
new
RenderContext
();
$results
=
$this
->
renderer
->
executeInRenderContext
(
$context
,
function
()
use
(
$query
)
{
return
$query
->
execute
();
});
if
(
!
$context
->
isEmpty
())
{
$query_cacheability
->
addCacheableDependency
(
$context
->
pop
());
}
return
$results
;
}
}
Loading