Skip to content

Commit ae04427

Browse files
committed
refactor: rename method names, delete unused variables and methods, implement dependency injection, and add translatino to options
1 parent 4e69088 commit ae04427

3 files changed

Lines changed: 62 additions & 54 deletions

File tree

media_fits.module

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
* Contains fits.module.
66
*/
77

8-
// phpcs:disable Drupal.Files.LineLength.TooLong
9-
// phpcs:disable Drupal.NamingConventions.ValidFunctionName.InvalidPrefix
10-
118
use Drupal\Core\Entity\EntityInterface;
129
use Drupal\Core\Routing\RouteMatchInterface;
1310
use Drupal\media\Entity\Media;
@@ -47,7 +44,7 @@ function media_fits_media_insert(EntityInterface $entity) {
4744
// Only extract Fits for File level only.
4845
if ('Media' === $entity->getEntityType()->getLabel()->getUntranslatedString()
4946
&& 1 === \Drupal::config('media_fits.fitsconfig')->get('fits-extract-ingesting')) {
50-
execute_media_fits_action($entity);
47+
media_fits_execute_media_fits_action($entity);
5148
}
5249
}
5350

@@ -57,7 +54,8 @@ function media_fits_media_insert(EntityInterface $entity) {
5754
/*function media_fits_media_update(EntityInterface $entity) {
5855
// Only extract Fits for File level only.
5956
if ('Media' === $entity->getEntityType()->getLabel()->getUntranslatedString()
60-
&& 1 === \Drupal::config('media_fits.fitsconfig')->get('fits-extract-ingesting')) {
57+
&& 1 === \Drupal::config('media_fits.fitsconfig')
58+
->get('fits-extract-ingesting')) {
6159

6260
$media = Media::load($entity->id());
6361
$source_field_name = $media->getSource()->getConfiguration()['source_field'];
@@ -66,23 +64,24 @@ $current_file_id = $media->get($source_field_name)->target_id;
6664
$previous_revision_id = $media->getRevisionId() - 1;
6765
$previous_revision = \Drupal::entityTypeManager()->getStorage('media')->loadRevision($previous_revision_id);
6866

69-
if (isset($source_field_name) && (isset($previous_revision) && $previous_revision->hasField($source_field_name))) {
67+
if (isset($source_field_name) && (isset($previous_revision) &&
68+
$previous_revision->hasField($source_field_name))) {
7069
$previous_file_id = $previous_revision->get($source_field_name)->target_id;
7170
} else {
7271
$previous_file_id = null; // or any other default value or action
7372
}
7473

7574
if ($current_file_id !== $previous_file_id) {
7675
// A new file ID was generated.
77-
execute_media_fits_action($entity);
76+
media_fits_execute_media_fits_action($entity);
7877
}
7978
}
8079
}*/
8180

8281
/**
8382
* Shared function call for execute Fits Action.
8483
*/
85-
function execute_media_fits_action(EntityInterface $entity) {
84+
function media_fits_execute_media_fits_action(EntityInterface $entity) {
8685
// Fix warning when Config form hasn't been setup.
8786
$config = \Drupal::config('media_fits.fitsconfig');
8887
if (!isset($config) || empty($config->get('fits-advancedqueue_id'))) {
@@ -92,38 +91,3 @@ function execute_media_fits_action(EntityInterface $entity) {
9291
$utils = \Drupal::service('media_fits.context_utils');
9392
$utils->executeFileReactions('\Drupal\media_fits\Plugin\ContextReaction\MediaFitsReaction', $media);
9493
}
95-
96-
if (!function_exists('print_log')) {
97-
98-
/**
99-
* Debug function: display any variable to error log.
100-
*/
101-
function print_log($thing) {
102-
error_log(print_r($thing, TRUE), 0);
103-
}
104-
105-
}
106-
107-
if (!function_exists('logging')) {
108-
109-
/**
110-
* Debug function: display any variable to current webpage.
111-
*/
112-
function logging($thing) {
113-
echo '<pre>';
114-
print_r($thing);
115-
echo '</pre>';
116-
}
117-
118-
}
119-
120-
if (!function_exists('drupal_log')) {
121-
122-
/**
123-
* Debug function: display any variable to drupal Reports Log messages.
124-
*/
125-
function drupal_log($msg) {
126-
\Drupal::logger(basename(__FILE__, '.module'))->error($msg);
127-
}
128-
129-
}

src/Form/MediaFitsConfigForm.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,64 @@
11
<?php
22

3-
// phpcs:disable DrupalPractice.General.OptionsT.TforValue
4-
// phpcs:disable DrupalPractice.Objects.GlobalDrupal.GlobalDrupal
5-
63
namespace Drupal\media_fits\Form;
74

85
use Drupal\Core\Form\ConfigFormBase;
96
use Drupal\Core\Form\FormStateInterface;
7+
use Drupal\Core\Config\ConfigFactoryInterface;
8+
use Drupal\Core\Entity\EntityFieldManagerInterface;
9+
use Drupal\Core\Entity\EntityTypeManagerInterface;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
1011

1112
/**
1213
* Class MediaFitsConfigForm definition.
1314
*/
1415
class MediaFitsConfigForm extends ConfigFormBase {
1516

17+
/**
18+
* The entity field manager service.
19+
*
20+
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
21+
*/
22+
protected $entityFieldManager;
23+
24+
/**
25+
* The entity type manager service.
26+
*
27+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
28+
*/
29+
protected $entityTypeManager;
30+
31+
/**
32+
* Constructs a MediaFitsConfigForm object.
33+
*
34+
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
35+
* The config factory service.
36+
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
37+
* The entity field manager service.
38+
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
39+
* The entity type manager service.
40+
*/
41+
public function __construct(
42+
ConfigFactoryInterface $config_factory,
43+
EntityFieldManagerInterface $entity_field_manager,
44+
EntityTypeManagerInterface $entity_type_manager,
45+
) {
46+
parent::__construct($config_factory);
47+
$this->entityFieldManager = $entity_field_manager;
48+
$this->entityTypeManager = $entity_type_manager;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public static function create(ContainerInterface $container) {
55+
return new static(
56+
$container->get('config.factory'),
57+
$container->get('entity_field.manager'),
58+
$container->get('entity_type.manager')
59+
);
60+
}
61+
1662
/**
1763
* {@inheritdoc}
1864
*/
@@ -51,9 +97,9 @@ public function buildForm(array $form, FormStateInterface $form_state) {
5197
'#type' => 'select',
5298
'#title' => 'Select Fits method:',
5399
'#options' => [
54-
0 => '-- Select --',
55-
'remote' => 'FITS Web Service',
56-
'local' => 'FITS from the command-line',
100+
0 => $this->t('-- Select --'),
101+
'remote' => $this->t('FITS Web Service'),
102+
'local' => $this->t('FITS from the command-line'),
57103
],
58104
'#required' => TRUE,
59105
'#ajax' => [
@@ -100,7 +146,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
100146
];
101147

102148
$queues = ['0' => "-- Select --"];
103-
$queues = array_merge($queues, \Drupal::entityQuery('advancedqueue_queue')->execute());
149+
$queues = array_merge($queues, $this->entityTypeManager->getStorage('advancedqueue_queue')->getQuery()->execute());
104150
$form['container']['fits-services-config']['op-config']['advancedqueue-id'] = [
105151
'#type' => 'select',
106152
'#name' => 'advancedqueue-id',
@@ -137,7 +183,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
137183
];
138184

139185
// Select default fits fields.
140-
$field_map = \Drupal::service('entity_field.manager')->getFieldMap();
186+
$field_map = $this->entityFieldManager->getFieldMap();
141187
$node_field_map = $field_map['file'];
142188
$fields = array_keys($node_field_map);
143189
$fits_fields = [];
@@ -189,7 +235,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
189235
* Query existing File types.
190236
*/
191237
public function getFileTypes() {
192-
$contentTypes = \Drupal::service('entity_type.manager')->getStorage('file_type')->loadMultiple();
238+
$contentTypes = $this->entityTypeManager->getStorage('file_type')->loadMultiple();
193239
$types = [];
194240
foreach ($contentTypes as $contentType) {
195241
$types[$contentType->id()] = $contentType->label();

src/Plugin/AdvancedQueue/JobType/MediaFitsJob.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public function process(Job $job) {
5454
*/
5555
public function extractFits($media = NULL) {
5656
/** @var \Drupal\media\MediaInterface $media */
57-
// phpcs:ignore -- Unused variable $config.
58-
$config = \Drupal::config('media_fits.fitsconfig');
5957
$report = "";
6058
$sucess = TRUE;
6159
if (!isset($media)) {

0 commit comments

Comments
 (0)