Skip to content

Commit

Permalink
Merge pull request #183 from esmero/ISSUE-182
Browse files Browse the repository at this point in the history
ISSUE-182: Macro issue, update to Drupal 10, PHP 8.1
  • Loading branch information
DiegoPino committed Oct 31, 2023
2 parents 6c5cf2b + 09f9492 commit 05d6a5b
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 62 deletions.
6 changes: 3 additions & 3 deletions ami.info.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Archipelago Multi Importer
description: Archipelago Multi Importer for Drupal 9. A module of mass ingest made for the masses.
description: Archipelago Multi Importer for Drupal 9/10. A module of mass ingest made for the masses.
package: Archipelago
type: module
core_version_requirement: ^8.9 || ^9
php: 7.1
core_version_requirement: ^9.5 || ^10
php: 8
dependencies:
- 'views_bulk_operations'
- 'strawberryfield'
Expand Down
5 changes: 3 additions & 2 deletions ami.module
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ function ami_form_metadatadisplay_entity_form_alter(&$form,FormStateInterface $f

$form['preview']['render_native'] = [
'#type' => 'checkbox',
'#weight' => 10,
'#defaut_value' => FALSE,
'#weight' => -6,
'#title' => 'Show Preview using native Output Format (e.g. HTML)',
'#states' => [
'visible' => [
Expand All @@ -140,6 +140,7 @@ function ami_form_metadatadisplay_entity_form_alter(&$form,FormStateInterface $f
$form['preview']['show_json_table'] = [
'#type' => 'checkbox',
'#defaut_value' => FALSE,
'#weight' => -5,
'#title' => 'Show Preview with JSON keys used in this template',
'#states' => [
'visible' => [
Expand Down Expand Up @@ -197,6 +198,6 @@ function ami_ami_set_entity_presave(Drupal\Core\Entity\EntityInterface $entity)
if ($sbf_fields = \Drupal::service('strawberryfield.utility')->bearsStrawberryfield($entity)) {
$event_type = AmiEventType::PRESAVE;
$event = new AmiCrudEvent($event_type, $entity, $sbf_fields);
\Drupal::service('event_dispatcher')->dispatch($event_type, $event);
\Drupal::service('event_dispatcher')->dispatch($event, $event_type);
}
}
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "archipelago/ami",
"description": "Archipelago Multi Importer for Drupal 9. A module of mass ingest/edits made for the masses",
"description": "Archipelago Multi Importer for Drupal 9/10. A module of mass ingest/edits made for the masses",
"type": "drupal-module",
"license": "GPL-2.0+",
"homepage": "https://github.com/esmero/ami",
Expand All @@ -13,17 +13,17 @@
}
],
"require": {
"drupal/core": "^9",
"drupal/core": "^9.5 || ^10",
"ext-zip": "*",
"ext-json": "*",
"drupal/views_bulk_operations": "4.1.2",
"strawberryfield/strawberryfield":"1.1.0.x-dev",
"strawberryfield/webform_strawberryfield":"1.1.0.x-dev",
"strawberryfield/format_strawberryfield":"1.1.0.x-dev",
"drupal/views_bulk_operations": "^4.2",
"strawberryfield/strawberryfield":"1.3.0.x-dev",
"strawberryfield/webform_strawberryfield":"1.3.0.x-dev",
"strawberryfield/format_strawberryfield":"1.3.0.x-dev",
"phpoffice/phpspreadsheet": "^1.15.0",
"maennchen/zipstream-php": "^1.2 || ^2.1",
"drupal/google_api_client": "^3.2 || ^4.3",
"ramsey/uuid": "^4.1",
"ramsey/uuid": "^4",
"monolog/monolog": "^2.9"
},
"minimum-stability": "dev",
Expand Down
69 changes: 47 additions & 22 deletions src/AmiUtilityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Ramsey\Uuid\Uuid;
use Drupal\Core\File\Exception\FileException;
use SplFileObject;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;

class AmiUtilityService {

Expand Down Expand Up @@ -560,7 +561,7 @@ public function retrieve_remote_file(
$mimefromextension = \Drupal::service(
'strawberryfield.mime_type.guesser.mime'
)
->guess($filename_from_remote ?? $path);
->guessMimeType($filename_from_remote ?? $path);
}

if (count($mimetype_array) && ($mimefromextension == NULL || $mimefromextension != $mimetype_array[0]) && ($mimetype_array[0] != 'application/octet-stream')) {
Expand All @@ -578,7 +579,7 @@ public function retrieve_remote_file(
// If none try with the filename either from remote (if set) of from the download path
if (!$extension || $extension == 'bin'){
$mimefromextension = \Drupal::service('strawberryfield.mime_type.guesser.mime')
->guess($filename_from_remote ?? $path);
->guessMimeType($filename_from_remote ?? $path);
if (($mimefromextension !== "application/octet-stream")) {
$extension = $extensions_from_remote ?? 'bin';
}
Expand Down Expand Up @@ -893,10 +894,8 @@ public function csv_touch(string $filename = NULL) {
return NULL;
}
}
// Ensure the file with empty data
$file = file_save_data(
'', $uri, FileSystemInterface::EXISTS_REPLACE
);

$file = \Drupal::service('file.repository')->writeData('', $uri, FileSystemInterface::EXISTS_REPLACE);

if (!$file) {
$this->messenger()->addError(
Expand Down Expand Up @@ -955,9 +954,8 @@ public function csv_save(array $data, $uuid_key = 'node_uuid', $auto_uuid = TRUE
return NULL;
}
// Ensure the file
$file = file_save_data(
'', $path . '/' . $filename, FileSystemInterface::EXISTS_REPLACE
);

$file = \Drupal::service('file.repository')->writeData('', $path . '/' . $filename, FileSystemInterface::EXISTS_REPLACE);
if (!$file) {
$this->messenger()->addError(
$this->t('Unable to create AMI CSV file. Verify permissions please.')
Expand Down Expand Up @@ -1020,7 +1018,7 @@ public function csv_save(array $data, $uuid_key = 'node_uuid', $auto_uuid = TRUE
$this->t(
'Your source data was saved and is available as CSV at. <a href="@url">@filename</a>.',
[
'@url' => file_create_url($file->getFileUri()),
'@url' => \Drupal::service('file_url_generator')->generateAbsoluteString($file->getFileUri()),
'@filename' => $file->getFilename(),
]
)
Expand Down Expand Up @@ -1190,10 +1188,14 @@ public function csv_read(File $file, int $offset = 0, int $count = 0, bool $alwa
$highestRow = count($data);
if ($always_include_header) {
$rowHeaders = $data[0] ?? [];
$rowHeaders_utf8 = array_map('stripslashes', $rowHeaders);
$rowHeaders_utf8 = array_map('utf8_encode', $rowHeaders_utf8);
$rowHeaders_utf8 = array_map('strtolower', $rowHeaders_utf8);
$rowHeaders_utf8 = array_map('trim', $rowHeaders_utf8);
$rowHeaders_utf8 = array_map(function($value) {
$value = $value ?? '';
$value = stripslashes($value);
$value = function_exists('mb_convert_encoding') ? mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1') : utf8_encode($value);
$value = strtolower($value);
$value = trim($value);
return $value;
}, $rowHeaders);
$headercount = count($rowHeaders);
}
else {
Expand Down Expand Up @@ -1430,7 +1432,10 @@ public function arrayEquallySeize($headercount, $row = []) {
public function getDifferentValuesfromColumn(array $data, int $key): array {
$unique = [];
$all = array_column($data['data'], $key);
$unique = array_map('trim', $all);
$unique = array_map(function($value) {
$value = $value ?? '';
return trim($value);
}, $all);
$unique = array_unique($unique, SORT_STRING);
return $unique;
}
Expand Down Expand Up @@ -1528,6 +1533,7 @@ public function getWebforms() {
$webform_ids = $query
->condition("status", "open")
->sort('title', 'ASC')
->accessCheck(TRUE)
->execute();
if (count($webform_ids)) {
$webforms = $this->entityTypeManager->getStorage('webform')->loadMultiple(
Expand Down Expand Up @@ -1677,7 +1683,14 @@ public function createAmiSet(\stdClass $data) {
$zipfail = TRUE;
}
else {
$zipfile = file_move($zipfile, $target_directory, FileSystemInterface::EXISTS_REPLACE);
/** @var \Drupal\file\FileRepositoryInterface $file_repository */
$file_repository = \Drupal::service('file.repository');
try {
$zipfile = $file_repository->move($zipfile, $target_directory, FileSystemInterface::EXISTS_RENAME);
}
catch (InvalidStreamWrapperException $e) {
$zipfail = TRUE;
}
if (!$zipfile) {
$zipfail = TRUE;
}
Expand Down Expand Up @@ -1778,7 +1791,10 @@ public function preprocessAmiSet(File $file, \stdClass $data, array &$invalid =
}
elseif (is_string($parent_ados_expanded) || is_integer($parent_ados_expanded)) {
// This allows single value and or ; and trims. Neat?
$parent_ados_array = array_map('trim', explode(';', $parent_ados_expanded));
$parent_ados_array = array_map(function($value) {
$value = $value ?? '';
return trim($value);
}, explode(';', $parent_ados_expanded));
}

$ado['parent'][$parent_key] = $parent_ados_array;
Expand Down Expand Up @@ -1868,9 +1884,10 @@ public function preprocessAmiSet(File $file, \stdClass $data, array &$invalid =
)
) {
// This allows single value and or ; and trims. Neat?
$parentup_array = array_map(
'trim', explode(';', $parentup_expanded)
);
$parentup_array = array_map(function($value) {
$value = $value ?? '';
return trim($value);
}, explode(';', $parentup_expanded));
}

foreach ($parentup_array as $parentup) {
Expand Down Expand Up @@ -2041,7 +2058,11 @@ protected function validateAmiSet(array $file_data_all, \stdClass $data, $strict
$required_headers = array_merge($required_headers, array_values((array)$data->column_keys));
}
// We use internally Lower case Headers.
$required_headers = array_map('strtolower', $required_headers);
$required_headers = array_map(function($value) {
$value = $value ?? '';
return strtolower($value);
}, $required_headers);

$headers_missing = array_diff(array_unique($required_headers), $file_data_all['headers']);
if (count($headers_missing)) {
$message = $this->t(
Expand Down Expand Up @@ -2488,7 +2509,11 @@ public function getDifferentValuesfromColumnSplit(array $data, int $key, array $
$all_entries[] = $chosen_entry;
}
}
$unique = array_map('trim', $all_entries);
$unique = array_map(function($value) {
$value = $value ?? '';
return trim($value);
}, $all_entries);

$unique = array_unique(array_values($unique), SORT_STRING);
return $unique;
}
Expand Down
23 changes: 19 additions & 4 deletions src/Form/amiSetEntityReconcileForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;

/**
* Form controller for the MetadataDisplayEntity entity delete form.
Expand Down Expand Up @@ -143,10 +144,24 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'Please use the Edit Reconciled LoD tab to Fix/Correct/Enhance or '
),
];
$form['status']['download'] = Url::fromUri(file_create_url($lod_file->getFileUri()))->toRenderArray();
$form['status']['download']['#type'] = 'link';
$form['status']['download']['#title'] = $this->t('Download LoD CSV');

/** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
$file_url_generator = \Drupal::service('file_url_generator');
try {
$lod_file_url = $file_url_generator->generateAbsoluteString(
$lod_file->getFileUri()
);
}
catch (InvalidStreamWrapperException $e) {
$lod_file_url = NULL;
}
if ($lod_file_url) {
$form['status']['download'] = Url::fromUri($lod_file_url)
->toRenderArray();
$form['status']['download']['#type'] = 'link';
$form['status']['download']['#title'] = $this->t(
'Download LoD CSV'
);
}
}
}
$form['mapping'] = [
Expand Down
18 changes: 11 additions & 7 deletions src/Plugin/Action/AmiStrawberryfieldCSVexport.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* {@inheritdoc}
*/
public function setContext(array &$context) {
public function setContext(array &$context):void {
$this->context['sandbox'] = &$context['sandbox'];
foreach ($context as $key => $item) {
if ($key === 'sandbox') {
Expand All @@ -184,7 +184,7 @@ public function setContext(array &$context) {
/**
* {@inheritdoc}
*/
public function setView(ViewExecutable $view) {
public function setView(ViewExecutable $view):void {
$this->view = $view;
}

Expand Down Expand Up @@ -335,8 +335,7 @@ public function execute($entity = NULL) {
return $row;
}


protected function generateOutput() {
protected function generateOutput():array {
$rows = [];
for ($i = 1; $i <= $this->context['sandbox']['current_batch']; $i++) {
$chunk = $this->tempStore->get($this->context['sandbox']['cid_prefix'] . $i);
Expand All @@ -358,7 +357,7 @@ protected function generateOutput() {
/**
* Output generated string to file. Message user.
*
* @param string $output
* @param array $output
* The string that will be saved to a file.
*/
protected function sendToFile($output) {
Expand Down Expand Up @@ -413,9 +412,14 @@ protected function sendToFile($output) {
}


public function buildPreConfigurationForm(array $element, array $values, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function buildPreConfigurationForm(array $element, array $values, FormStateInterface $form_state):array {
return $element;
}


public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['expand_nodes_to_uuids'] = [
'#type' => 'checkbox',
Expand Down Expand Up @@ -464,7 +468,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
public function validateConfigurationForm(array &$form, FormStateInterface $form_state):void {

}

Expand Down
7 changes: 4 additions & 3 deletions src/Plugin/Action/AmiStrawberryfieldJsonAsText.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AmiStrawberryfieldJsonAsText extends StrawberryfieldJsonPatch implements V
/**
* {@inheritdoc}
*/
public function setContext(array &$context) {
public function setContext(array &$context):void {
$this->context['sandbox'] = &$context['sandbox'];
foreach ($context as $key => $item) {
if ($key === 'sandbox') {
Expand All @@ -71,7 +71,7 @@ public function setContext(array &$context) {
/**
* {@inheritdoc}
*/
public function setView(ViewExecutable $view) {
public function setView(ViewExecutable $view):void {
$this->view = $view;
}

Expand Down Expand Up @@ -229,7 +229,8 @@ public function execute($entity = NULL) {
}


public function buildPreConfigurationForm(array $element, array $values, FormStateInterface $form_state) {
public function buildPreConfigurationForm(array $element, array $values, FormStateInterface $form_state):array {
return $element;
}

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
Expand Down
5 changes: 3 additions & 2 deletions src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static function create(ContainerInterface $container, array $configuratio

}

public function buildPreConfigurationForm(array $element, array $values, FormStateInterface $form_state) {

public function buildPreConfigurationForm(array $element, array $values, FormStateInterface $form_state):array {
return $element;
}

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
Expand Down Expand Up @@ -304,6 +304,7 @@ public function execute($entity = NULL) {
// Now try to decode fullvalues
$fullvaluesmodified_string = json_encode($fullvaluesmodified);
$fullvaluesoriginal_string = json_encode($fullvaluesoriginal);
$json_error = json_last_error();
if ($json_error != JSON_ERROR_NONE) {
$visualjsondiff = new Diff(explode(PHP_EOL, $fullvaluesmodified_string), explode(PHP_EOL,$fullvaluesoriginal_string));
$formatter = new DiffFormatter();
Expand Down
Loading

0 comments on commit 05d6a5b

Please sign in to comment.