Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
search_api_attachments
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
search_api_attachments
Commits
38e7a9e4
Commit
38e7a9e4
authored
3 months ago
by
Ismaeil ABOULJAMAL
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3409937
by shaxa, ismaelromero, izus: Create extra field for search api attachment content
parent
eeff76d4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
search_api_attachments.module
+64
-0
64 additions, 0 deletions
search_api_attachments.module
with
64 additions
and
0 deletions
search_api_attachments.module
+
64
−
0
View file @
38e7a9e4
...
...
@@ -5,8 +5,11 @@
* Implement hooks and help functions to delete extracted files cache content.
*/
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Entity\Display\EntityViewDisplayInterface
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Drupal\file\Entity\File
;
use
Drupal\search_api
\Entity\Index
;
/**
* Implements hook_ENTITY_TYPE_update().
...
...
@@ -81,3 +84,64 @@ function search_api_attachments_help($route_name, RouteMatchInterface $route_mat
}
return
NULL
;
}
/**
* Implements hook_entity_extra_field_info().
*/
function
search_api_attachments_entity_extra_field_info
()
{
$extra
=
[];
$entity_types
=
\Drupal
::
entityTypeManager
()
->
getDefinitions
();
$bundle_info
=
\Drupal
::
getContainer
()
->
get
(
'entity_type.bundle.info'
);
foreach
(
$entity_types
as
$entity_type_id
=>
$entity_type
)
{
if
(
$entity_type
instanceof
ContentEntityType
)
{
$bundles
=
$bundle_info
->
getBundleInfo
(
$entity_type_id
);
foreach
(
$bundles
as
$bundle
=>
$data
)
{
$extra
[
$entity_type_id
][
$bundle
][
'display'
][
'search_api_attachments'
]
=
[
'label'
=>
t
(
'Search api attachments'
),
'description'
=>
t
(
'An attachments field.'
),
'weight'
=>
100
,
'visible'
=>
FALSE
,
];
}
}
}
return
$extra
;
}
/**
* Implements hook_entity_view().
*/
function
search_api_attachments_entity_view
(
array
&
$build
,
EntityInterface
$entity
,
EntityViewDisplayInterface
$display
,
$view_mode
)
{
$attacments_component
=
$display
->
getComponent
(
'search_api_attachments'
);
if
(
$attacments_component
!==
NULL
)
{
$indexes
=
Index
::
loadMultiple
();
foreach
(
$indexes
as
$index
)
{
$index_fields
=
array_keys
(
$index
->
getFields
());
$fields
=
array_filter
(
$index_fields
,
fn
(
$element
)
=>
str_starts_with
(
$element
,
'saa_'
)
?
$element
:
NULL
);
if
(
$fields
)
{
foreach
(
$fields
as
$field
)
{
(
$query
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
'search_api_index'
)
->
load
(
$index
->
id
())
->
query
()
)
->
addCondition
(
'nid'
,
$entity
->
id
());
$items
=
$query
->
execute
()
->
getResultItems
();
$item
=
reset
(
$items
);
$content
=
$item
->
getField
(
$field
)
->
getValues
()[
0
];
$build
[
$field
]
=
[
'#plain_text'
=>
$content
->
getText
(),
];
}
}
}
}
}
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