Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
markdownify
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
markdownify
Commits
0f45ca06
Commit
0f45ca06
authored
2 months ago
by
Adrian M.
Committed by
Christoph Weber
2 months ago
Browse files
Options
Downloads
Patches
Plain Diff
#3527544
- Improve Hook Alterations and Cache Metadata Handling in Markdownify Module
parent
ddc8e9ee
No related branches found
No related tags found
1 merge request
!6
#3527544 - Improve Hook Alterations and Cache Metadata Handling in Markdownify Module
Pipeline
#511177
passed
2 months ago
Stage: build
Stage: validate
Changes
2
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
markdownify.api.php
+30
-1
30 additions, 1 deletion
markdownify.api.php
src/Service/MarkdownifyEntityRenderer.php
+28
-19
28 additions, 19 deletions
src/Service/MarkdownifyEntityRenderer.php
with
58 additions
and
20 deletions
markdownify.api.php
+
30
−
1
View file @
0f45ca06
...
...
@@ -5,6 +5,8 @@
* Hooks specific to the Markdownify module.
*/
use
Drupal\Core\Render\BubbleableMetadata
;
/**
* Alter the supported entity types for Markdown conversion.
*
...
...
@@ -27,6 +29,31 @@ function hook_markdownify_supported_entity_types_alter(array &$supported_entity_
}
}
/**
* Alter the render array of an entity before rendering to HTML.
*
* This hook allows other modules to modify the render array of an entity
* before it is rendered to HTML during Markdown transformation.
*
* @param array &$build
* The render array for the entity.
* @param array $context
* An associative array containing additional context:
* - entity: The entity being rendered (\Drupal\Core\Entity\EntityInterface).
* - view_mode: The view mode used to render the entity (string).
* - langcode: The language code for rendering (string).
* @param \Drupal\Core\Render\BubbleableMetadata|null $metadata
* (optional) Cacheable metadata for the rendering process.
*/
function
hook_markdownify_entity_build_alter
(
array
&
$build
,
array
$context
,
?BubbleableMetadata
$metadata
):
void
{
// Example: Add a custom class to all entities of type 'node'.
$entity
=
$context
[
'entity'
];
// Example: Add metadata to cache tags for 'article' node bundles.
if
(
$entity
->
getEntityTypeId
()
===
'node'
&&
$entity
->
bundle
()
===
'article'
&&
$metadata
)
{
$metadata
->
addCacheTags
([
'custom_article_tag'
]);
}
}
/**
* Alter the rendered HTML of an entity before conversion to Markdown.
*
...
...
@@ -40,8 +67,10 @@ function hook_markdownify_supported_entity_types_alter(array &$supported_entity_
* - entity: The entity being converted (\Drupal\Core\Entity\EntityInterface).
* - view_mode: The view mode used to render the entity (string).
* - langcode: The language code of the entity's content (string).
* @param \Drupal\Core\Render\BubbleableMetadata|null $metadata
* (optional) Cacheable metadata for the rendering process.
*/
function
hook_markdownify_entity_html_alter
(
string
&
$html
,
array
$context
):
void
{
function
hook_markdownify_entity_html_alter
(
string
&
$html
,
array
$context
,
?BubbleableMetadata
$metadata
):
void
{
// Example: Add a custom wrapper around the HTML.
$html
=
'<div class="custom-wrapper">'
.
$html
.
'</div>'
;
// Example: Modify the HTML for a specific entity type.
...
...
This diff is collapsed.
Click to expand it.
src/Service/MarkdownifyEntityRenderer.php
+
28
−
19
View file @
0f45ca06
...
...
@@ -76,15 +76,15 @@ class MarkdownifyEntityRenderer implements MarkdownifyEntityRendererInterface {
*/
public
function
toHtml
(
EntityInterface
$entity
,
string
$view_mode
=
'full'
,
?string
$langcode
=
NULL
,
?BubbleableMetadata
$metadata
=
NULL
):
string
{
try
{
// Set the render context.
// Set the render context
to capture cacheable metadata
.
$context
=
new
RenderContext
();
// Render entity in context and collect cacheable metadata.
$html
=
$this
->
renderer
->
executeInRenderContext
(
$context
,
function
()
use
(
$entity
,
$view_mode
,
$langcode
)
{
return
$this
->
renderEntity
(
$entity
,
$view_mode
,
$langcode
);
$html
=
$this
->
renderer
->
executeInRenderContext
(
$context
,
function
()
use
(
$entity
,
$view_mode
,
$langcode
,
$metadata
)
{
return
$this
->
renderEntity
(
$entity
,
$view_mode
,
$langcode
,
$metadata
);
});
// Merge any bubbled cacheable metadata.
$this
->
applyCacheableMetadata
(
$entity
,
$context
,
$metadata
);
// Return the rendered output.
// Return
s
the
fully
rendered
HTML
output.
return
$html
;
}
catch
(
\Exception
$e
)
{
...
...
@@ -95,37 +95,46 @@ class MarkdownifyEntityRenderer implements MarkdownifyEntityRendererInterface {
}
/**
* Builds and renders the entity view array.
* Builds and renders the entity view array
into an HTML string
.
*
* This method
constructs the render array for the entity using the specified
*
view mode and language, then renders it to
an HTML string
. Other modules
* ca
n alter the final output using the 'markdownify_entity_html' alter hook
.
* This method
integrates with Drupal's rendering pipeline, invokes alter
*
hooks for both render arrays
an
d
HTML string
s, and ensures proper
* ca
che metadata handling
.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to render.
* @param string $view_mode
* The view mode
to use
.
* The view mode
for rendering the entity
.
* @param string|null $langcode
* The language code to render in.
* (optional) The language code for rendering.
* @param \Drupal\Core\Render\BubbleableMetadata|null $metadata
* (optional) Cacheable metadata for the rendering process.
*
* @return string
* The rendered HTML output.
*/
protected
function
renderEntity
(
EntityInterface
$entity
,
string
$view_mode
,
?string
$langcode
):
string
{
protected
function
renderEntity
(
EntityInterface
$entity
,
string
$view_mode
,
?string
$langcode
,
?BubbleableMetadata
$metadata
=
NULL
):
string
{
// Context for hook alterations.
$alter_context
=
[
'entity'
=>
$entity
,
'view_mode'
=>
$view_mode
,
'langcode'
=>
$langcode
,
];
// Obtain the appropriate view builder for the entity type.
$view_builder
=
$this
->
entityTypeManager
->
getViewBuilder
(
$entity
->
getEntityTypeId
());
// Build the render array for the entity.
$build
=
$view_builder
->
view
(
$entity
,
$view_mode
,
$langcode
);
// Allow modules to alter the render array before rendering as HTML.
$this
->
moduleHandler
->
alter
(
'markdownify_entity_build'
,
$build
,
$alter_context
,
$metadata
);
// Render the build array into an HTML string.
$html
=
$this
->
renderer
->
render
(
$build
);
$html
=
$this
->
renderer
->
render
(
$build
,
TRUE
);
// Allow modules to alter the rendered HTML output.
$alter_context
=
[
'entity'
=>
$entity
,
'view_mode'
=>
$view_mode
,
'langcode'
=>
$langcode
,
];
$this
->
moduleHandler
->
alter
(
'markdownify_entity_html'
,
$html
,
$alter_context
);
// Return the rendered output.
$this
->
moduleHandler
->
alter
(
'markdownify_entity_html'
,
$html
,
$alter_context
,
$metadata
);
// Adds cacheable metadata to ensure proper caching of the rendered output.
if
(
$metadata
instanceof
BubbleableMetadata
)
{
$metadata
->
addCacheableDependency
(
$build
);
}
// Returns the fully rendered HTML output.
return
$html
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment