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
Merge requests
!7
Migrate to new hook
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Migrate to new hook
issue/markdownify-3526226:3526226-create-submodule-for
into
1.1.x
Overview
18
Commits
2
Pipelines
8
Changes
2
All threads resolved!
Show all comments
Merged
Migrate to new hook
Arash Poorakbar
requested to merge
issue/markdownify-3526226:3526226-create-submodule-for
into
1.1.x
3 months ago
Overview
18
Commits
2
Pipelines
8
Changes
2
All threads resolved!
Show all comments
Closes
#3526226
Edited
3 months ago
by
Arash Poorakbar
0
0
Merge request reports
Compare
1.1.x
version 6
db4b187c
3 months ago
version 5
87768bb0
3 months ago
version 4
952f987b
3 months ago
version 3
b7321c4f
3 months ago
version 2
b5497b07
3 months ago
version 1
0060afad
3 months ago
1.1.x (base)
and
latest version
latest version
5f08af5b
2 commits,
3 months ago
version 6
db4b187c
1 commit,
3 months ago
version 5
87768bb0
1 commit,
3 months ago
version 4
952f987b
1 commit,
3 months ago
version 3
b7321c4f
1 commit,
3 months ago
version 2
b5497b07
22 commits,
3 months ago
version 1
0060afad
22 commits,
3 months ago
2 files
+
174
−
63
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
modules/file_attachment/src/Plugin/Field/FieldFormatter/MdFileAttachmentFieldFormatter.php
0 → 100644
+
152
−
0
View file @ 5f08af5b
Edit in single-file editor
Open in Web IDE
Show comments on this file
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\markdownify_file_attachment\Plugin\Field\FieldFormatter
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\File\FileSystemInterface
;
use
Drupal\Core\File\FileUrlGeneratorInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Render\RenderContext
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase
;
use
Drupal\file\Plugin\Field\FieldType\FileFieldItemList
;
use
Psr\Log\LoggerInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Plugin implementation of the 'entity_reference_file_attachment' formatter.
*
* @FieldFormatter(
* id = "md_file_attachment_file_embed",
* label = @Translation("Embed file as Markdown"),
* field_types = {
* "file"
* }
* )
*/
final
class
MdFileAttachmentFieldFormatter
extends
FileFormatterBase
implements
ContainerFactoryPluginInterface
{
/**
* File URL generator.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
private
FileUrlGeneratorInterface
$fileUrlGenerator
;
/**
* Renderer interface.
*
* @var \Drupal\Core\Render\RendererInterface
*/
private
RendererInterface
$renderer
;
/**
* Logger interface.
*
* @var \Psr\Log\LoggerInterface
*/
private
LoggerInterface
$logger
;
/**
* File system interface.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
private
FileSystemInterface
$fileSystem
;
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
):
self
{
$instance
=
new
self
(
$plugin_id
,
$plugin_definition
,
$configuration
[
'field_definition'
],
$configuration
[
'settings'
],
$configuration
[
'label'
],
$configuration
[
'view_mode'
],
$configuration
[
'third_party_settings'
],
);
$instance
->
fileUrlGenerator
=
$container
->
get
(
'file_url_generator'
);
$instance
->
renderer
=
$container
->
get
(
'renderer'
);
$instance
->
logger
=
$container
->
get
(
'logger.channel.markdownify'
);
$instance
->
fileSystem
=
$container
->
get
(
'file_system'
);
return
$instance
;
}
/**
* {@inheritdoc}
*/
public
static
function
defaultSettings
():
array
{
return
[
'allowed_extensions'
=>
[
'yml'
,
'txt'
],
'max_size'
=>
1024
,
];
}
/**
* {@inheritdoc}
*/
public
function
viewElements
(
FieldItemListInterface
$items
,
$langcode
):
array
{
assert
(
$items
instanceof
FileFieldItemList
);
$element
=
[];
$allowed_extensions
=
$this
->
getSetting
(
'allowed_extensions'
);
$max_size
=
$this
->
getSetting
(
'max_size'
);
foreach
(
$this
->
getEntitiesToView
(
$items
,
$langcode
)
as
$delta
=>
$file
)
{
if
(
!
$file
)
{
continue
;
}
$file_size
=
$file
->
getSize
();
$file_name
=
$file
->
getFilename
();
$extension
=
strtolower
(
pathinfo
(
$file_name
,
PATHINFO_EXTENSION
));
$uri
=
$file
->
getFileUri
();
$url
=
''
;
try
{
$context
=
new
RenderContext
();
$url
=
$this
->
renderer
->
executeInRenderContext
(
$context
,
function
()
use
(
$uri
)
{
return
$this
->
fileUrlGenerator
->
generateAbsoluteString
(
$uri
);
});
}
catch
(
\Exception
$e
)
{
$this
->
logger
->
error
(
'Failed to render file URL: @message'
,
[
'@message'
=>
$e
->
getMessage
()]);
}
$real_path
=
$this
->
fileSystem
->
realpath
(
$uri
);
if
(
in_array
(
$extension
,
$allowed_extensions
,
TRUE
)
&&
$file_size
<=
$max_size
)
{
$content
=
@
file_get_contents
(
$real_path
);
$element
[
$delta
]
=
[
'#type'
=>
'markup'
,
'#markup'
=>
$this
->
t
(
'Attached file %filename with %extension extension available at %url follows:<br>%file'
,
[
'%filename'
=>
$file_name
,
'%extension'
=>
$extension
,
'%file'
=>
$content
,
'%url'
=>
$url
,
]),
];
}
else
{
$element
[
$delta
]
=
[
'#type'
=>
'markup'
,
'#markup'
=>
$this
->
t
(
'Attached file %filename with %extension extension available at %url'
,
[
'%filename'
=>
$file_name
,
'%extension'
=>
$extension
,
'%url'
=>
$url
,
]),
];
}
}
return
$element
;
}
}
Loading