Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out drupal_static for resource display #3965

Merged
merged 5 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion modules/datastore/src/Form/ResourceSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\datastore\Form;

use Drupal\common\DataResource;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

Expand All @@ -24,7 +25,7 @@ public function getFormId() {
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['datastore.settings'];
return ['datastore.settings', 'metastore.settings'];
}

/**
Expand Down Expand Up @@ -52,6 +53,18 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => $this->config('datastore.settings')->get('delete_local_resource'),
'#description' => $this->t('Delete local copy of remote files after the datastore import is complete'),
];
$form['resource_perspective_display'] = [
'#type' => 'select',
'#title' => $this->t('Resource download url display'),
'#description' => $this->t('Choose to display either the source or local path to a resource file in the
metadata. Note that "Local URL" display only makes sense if "Delete local resource" is unchecked.'),
'#options' => [
DataResource::DEFAULT_SOURCE_PERSPECTIVE => $this->t('Source'),
'local_url' => $this->t('Local URL'),
],
'#default_value' => $this->config('metastore.settings')->get('resource_perspective_display')
?: DataResource::DEFAULT_SOURCE_PERSPECTIVE,
];
return parent::buildForm($form, $form_state);
}

Expand All @@ -64,6 +77,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('purge_file', $form_state->getValue('purge_file'))
->set('delete_local_resource', $form_state->getValue('delete_local_resource'))
->save();
$this->config('metastore.settings')
->set('resource_perspective_display', $form_state->getValue('resource_perspective_display'))
->save();
parent::submitForm($form, $form_state);
}

Expand Down
1 change: 1 addition & 0 deletions modules/metastore/config/install/metastore.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ property_list:
'spatial': 0
'temporal': 0
'isPartOf': 0
resource_perspective_display: source
5 changes: 4 additions & 1 deletion modules/metastore/config/schema/metastore.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ metastore.settings:
label: 'Property List'
sequence:
type: string
label: 'Property'
label: 'Property'
resource_perspective_display:
type: string
label: 'Resource download url display'
8 changes: 0 additions & 8 deletions modules/metastore/metastore.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @file
*/

use Drupal\common\DataResource;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
Expand Down Expand Up @@ -53,13 +52,6 @@ function metastore_entity_bundle_field_info_alter(&$fields, EntityTypeInterface
}
}

/**
* Helper method to retrieve the static value for a resource's display.
*/
function resource_mapper_display() {
return drupal_static('metastore_resource_mapper_display', DataResource::DEFAULT_SOURCE_PERSPECTIVE);
}

/**
* Helper method to retrieve the static value for a resource's revisioning.
*/
Expand Down
1 change: 1 addition & 0 deletions modules/metastore/metastore.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
- '@date.formatter'
- '@dkan.metastore.storage'
- '@queue'
- '@config.factory'
dkan.metastore.schema_retriever:
class: \Drupal\metastore\SchemaRetriever
arguments:
Expand Down
15 changes: 13 additions & 2 deletions modules/metastore/src/LifeCycle/LifeCycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\common\EventDispatcherTrait;
use Drupal\common\DataResource;
use Drupal\common\UrlHostTokenResolver;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Queue\QueueFactory;
use Drupal\metastore\MetastoreItemInterface;
Expand Down Expand Up @@ -72,6 +73,13 @@ class LifeCycle {
*/
protected $queueFactory;

/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;

/**
* Constructor.
*/
Expand All @@ -82,7 +90,8 @@ public function __construct(
ResourceMapper $resourceMapper,
DateFormatter $dateFormatter,
DataFactory $dataFactory,
QueueFactory $queueFactory
QueueFactory $queueFactory,
ConfigFactory $configFactory
) {
$this->referencer = $referencer;
$this->dereferencer = $dereferencer;
Expand All @@ -91,6 +100,7 @@ public function __construct(
$this->dateFormatter = $dateFormatter;
$this->dataFactory = $dataFactory;
$this->queueFactory = $queueFactory;
$this->configFactory = $configFactory;
}

/**
Expand Down Expand Up @@ -227,7 +237,8 @@ private function retrieveDownloadUrlFromResourceMapper(string $resourceIdentifie
}

$reference[] = $this->createResourceReference($sourceResource);
$perspective = \resource_mapper_display();
$perspective = $this->configFactory->get('metastore.settings')->get('resource_perspective_display')
?: DataResource::DEFAULT_SOURCE_PERSPECTIVE;
$resource = $sourceResource;

if (
Expand Down
6 changes: 4 additions & 2 deletions tests/src/Functional/DatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,10 @@ private function datastoreImportAndQuery() {
}

private function changeDatasetsResourceOutputPerspective(string $perspective = DataResource::DEFAULT_SOURCE_PERSPECTIVE) {
$display = &drupal_static('metastore_resource_mapper_display');
$display = $perspective;
$configFactory = \Drupal::service('config.factory');
$config = $configFactory->getEditable('metastore.settings');
$config->set('resource_perspective_display', $perspective);
$config->save();
}

private function getResourceDatastoreTable(object $resource) {
Expand Down