Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
filefield_paths
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
filefield_paths
Merge requests
!25
Issue
#2930945
: Port Drush integration for File (Field) Paths
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Issue
#2930945
: Port Drush integration for File (Field) Paths
issue/filefield_paths-2930945:2930945-port-drush-integration
into
8.x-1.x
Overview
0
Commits
5
Pipelines
8
Changes
6
Merged
Oleh Vehera
requested to merge
issue/filefield_paths-2930945:2930945-port-drush-integration
into
8.x-1.x
6 months ago
Overview
0
Commits
5
Pipelines
8
Changes
6
Expand
Closes
#2930945
0
0
Merge request reports
Compare
8.x-1.x
version 7
37ba6d8e
5 months ago
version 6
76147092
5 months ago
version 5
1ccc83c4
5 months ago
version 4
e566f389
5 months ago
version 3
38d33a8d
5 months ago
version 2
3baad391
5 months ago
version 1
3d948091
6 months ago
8.x-1.x (base)
and
latest version
latest version
37ba6d8e
5 commits,
5 months ago
version 7
37ba6d8e
5 commits,
5 months ago
version 6
76147092
4 commits,
5 months ago
version 5
1ccc83c4
4 commits,
5 months ago
version 4
e566f389
4 commits,
5 months ago
version 3
38d33a8d
4 commits,
5 months ago
version 2
3baad391
3 commits,
5 months ago
version 1
3d948091
1 commit,
6 months ago
6 files
+
548
−
263
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
6
Search (e.g. *.vue) (Ctrl+P)
src/Batch/Updater.php
0 → 100644
+
122
−
0
Options
<?php
namespace
Drupal\filefield_paths\Batch
;
use
Drupal\Core\Batch\BatchBuilder
;
use
Drupal\Core\DependencyInjection\DependencySerializationTrait
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\StringTranslation\StringTranslationTrait
;
use
Drupal\field\FieldConfigInterface
;
/**
* File (Field) Paths Batch Updater service.
*/
class
Updater
{
use
StringTranslationTrait
;
use
DependencySerializationTrait
;
/**
* Constructs a new FileFieldPathBatchUpdater object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
*/
public
function
__construct
(
protected
readonly
EntityTypeManagerInterface
$entityTypeManager
,
)
{}
/**
* Set batch process to update File (Field) Paths.
*
* @param \Drupal\field\FieldConfigInterface $field_config
* The file field for which to update paths.
*
* @return bool
* True if there were paths to update, false otherwise.
*/
public
function
batchUpdate
(
FieldConfigInterface
$field_config
):
bool
{
$entity_info
=
$this
->
entityTypeManager
->
getDefinition
(
$field_config
->
getTargetEntityTypeId
());
$query
=
$this
->
entityTypeManager
->
getStorage
(
$field_config
->
getTargetEntityTypeId
())
->
getQuery
();
if
(
$bundle_field
=
$entity_info
->
getKey
(
'bundle'
))
{
$query
->
condition
(
$bundle_field
,
$field_config
->
getTargetBundle
());
}
$result
=
$query
->
accessCheck
(
FALSE
)
->
addTag
(
'DANGEROUS_ACCESS_CHECK_OPT_OUT'
)
->
condition
(
"
{
$field_config
->
getName
()
}
.target_id"
,
''
,
'<>'
)
->
execute
();
// If there are no results, do not set a batch as there is nothing
// to process.
if
(
empty
(
$result
))
{
return
FALSE
;
}
// Create batch.
$batch
=
(
new
BatchBuilder
())
->
setTitle
(
$this
->
t
(
'Updating File (Field) Paths'
));
$batch
->
addOperation
(
[
$this
,
'batchProcess'
],
[
$result
,
$field_config
]
);
batch_set
(
$batch
->
toArray
());
return
TRUE
;
}
/**
* Batch callback for File (Field) Paths retroactive updates.
*
* @param int[] $objects
* A list of entity ID's for the entity type that the field is attached to.
* @param \Drupal\field\FieldConfigInterface $field_config
* The file field for which to update paths.
* @param array $context
* The batch context.
*
* @internal
*/
public
function
batchProcess
(
array
$objects
,
FieldConfigInterface
$field_config
,
array
&
$context
):
void
{
if
(
!
isset
(
$context
[
'sandbox'
][
'progress'
]))
{
$context
[
'sandbox'
][
'progress'
]
=
0
;
$context
[
'sandbox'
][
'max'
]
=
count
(
$objects
);
$context
[
'sandbox'
][
'objects'
]
=
$objects
;
}
/** @var \Drupal\Core\Entity\ContentEntityStorageBase $entity_storage */
$entity_storage
=
$this
->
entityTypeManager
->
getStorage
(
$field_config
->
getTargetEntityTypeId
());
// Process nodes by groups of 5.
$count
=
min
(
5
,
count
(
$context
[
'sandbox'
][
'objects'
]));
for
(
$i
=
1
;
$i
<=
$count
;
$i
++
)
{
// For each oid, load the object, update the files and save it.
$oid
=
array_shift
(
$context
[
'sandbox'
][
'objects'
]);
$entity
=
$entity_storage
->
load
(
$oid
);
// Enable active updating if it isn't already enabled.
$active_updating
=
$field_config
->
getThirdPartySetting
(
'filefield_paths'
,
'active_updating'
);
if
(
!
$active_updating
)
{
$field_config
->
setThirdPartySetting
(
'filefield_paths'
,
'active_updating'
,
TRUE
);
$field_config
->
save
();
}
$entity
->
original
=
$entity
;
filefield_paths_entity_update
(
$entity
);
// Restore active updating to it's previous state if necessary.
if
(
!
$active_updating
)
{
$field_config
->
setThirdPartySetting
(
'filefield_paths'
,
'active_updating'
,
$active_updating
);
$field_config
->
save
();
}
// Update our progress information.
$context
[
'sandbox'
][
'progress'
]
++
;
}
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if
(
$context
[
'sandbox'
][
'progress'
]
!==
$context
[
'sandbox'
][
'max'
])
{
$context
[
'finished'
]
=
$context
[
'sandbox'
][
'progress'
]
/
$context
[
'sandbox'
][
'max'
];
}
}
}
Loading