Skip to content

Commit

Permalink
Refactoring for Drupal 10. Added dependency injection. Remove comment…
Browse files Browse the repository at this point in the history
…ed out code not in use and clean code
  • Loading branch information
magnarem committed Nov 10, 2023
1 parent 80e7bf3 commit 87d73bb
Show file tree
Hide file tree
Showing 25 changed files with 347 additions and 574 deletions.
104 changes: 26 additions & 78 deletions metsis/metsis_lib/src/Controller/DynamicLandingPagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\search_api\Entity\Index;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
Expand Down Expand Up @@ -72,6 +71,16 @@ class DynamicLandingPagesController extends ControllerBase {
*/
protected $currentUser;

/**
* The leaflet map service.
*
* @var \Drupal\leaflet\LeafletService
*/
protected $leaflet;

/**
* License constant.
*/
public const LICENCES = [
'CC0-1.0' => [
'url' => 'https://spdx.org/licenses/CC0-1.0',
Expand Down Expand Up @@ -127,6 +136,7 @@ public static function create(ContainerInterface $container) {
$instance->json = $container->get('serialization.json');
$instance->geoPhpWrapper = $container->get('geofield.geophp');
$instance->request = $container->get('request_stack')->getCurrentRequest();
$instance->leaflet = $container->get('leaflet.service');
return $instance;
}

Expand All @@ -136,7 +146,7 @@ public static function create(ContainerInterface $container) {
* @return string
* Return Hello string.
*/
public function getLandingPage($id) {
public function getLandingPage(string $id, Request $request) {
// Get the host of this drupal instance.
$host = $this->request->getHost();
$fullhost = $this->request->getSchemeAndHttpHost();
Expand Down Expand Up @@ -171,9 +181,9 @@ public function getLandingPage($id) {
$fields = $doc->getFields();
}
// dpm($fields);
if (NULL != \Drupal::request()->query->get('export_type')) {
if (NULL != $request->query->get('export_type')) {
$response = new Response();
$export_type = \Drupal::request()->query->get('export_type');
$export_type = $request->query->get('export_type');
$id = $fields['id'];
$mmd = $fields['mmd_xml_file'];
// By setting these 2 header options, the browser will see the URL
Expand Down Expand Up @@ -265,7 +275,7 @@ public function getLandingPage($id) {
// $settings['map_position']['center']['lat'] = $features['lat'];
// $settings['map_position']['center']['lon'] = $features['lon'];
// Set $map array with leafletMapGetInfo.
$map = \Drupal::service('leaflet.service')->leafletMapGetInfo();
$map = $this->leaflet->leafletMapGetInfo();
// dpm($map);
// $map = leaflet_leaflet_map_info();
$map['OSM Mapnik']['settings']['leaflet_markercluster'] = [
Expand All @@ -283,12 +293,12 @@ public function getLandingPage($id) {
// dpm($features);
// $map['settings']['zoom'] = 1;
// render the map.
$map_result = \Drupal::service('leaflet.service')->leafletRenderMap($map['OSM Mapnik'], $features, $height = '400px');
$map_result = $this->leaflet->leafletRenderMap($map['OSM Mapnik'], $features, $height = '400px');
// Add the rendered map to the renderArray.
$renderArray['map'] = $map_result;

// Get the extent form for displaying temporal and geographial extent in tabs.
$renderArray['extentGroup'] = \Drupal::formBuilder()->getForm('Drupal\metsis_lib\Form\ExtentForm', $fields, $features, $isPoint);
// Get the form for displaying temporal and geographial extent in tabs.
$renderArray['extentGroup'] = $this->formBuilder()->getForm('Drupal\metsis_lib\Form\ExtentForm', $fields, $features, $isPoint);

/*
* Dataset Citation
Expand Down Expand Up @@ -368,13 +378,6 @@ public function getLandingPage($id) {
}
if (isset($fields['use_constraint_identifier'])) {
if (NULL != self::LICENCES[$fields['use_constraint_identifier']]) {
/* $renderArray['constraints_and_info']['constraints']['licence_identifier'] = [
'#type' => 'item',
'#title' => $this->t('Licence:'),
'#markup' => '<a class="w3-text-blue" href="'.self::LICENCES[$fields['use_constraint_identifier']]['url'].'">' .$fields['use_constraint_identifier'].'</a>',
'#allowed_tags' => ['a', 'strong'],
];
*/
$renderArray['constraints_and_info']['constraints']['licence_img'] = [
'#type' => 'markup',
// '#prefix' => '<p>',.
Expand Down Expand Up @@ -454,7 +457,6 @@ public function getLandingPage($id) {
'#allowed_tags' => ['a', 'strong', 'br'],

];
// $renderArray['constraints_and_info']['metadata_information']['metadata_download_actions'] = \Drupal::formBuilder()->getForm('Drupal\metsis_lib\Form\ExportForm', $fields);.
/*
* DATA ACCESS
*/
Expand Down Expand Up @@ -739,7 +741,7 @@ public function getLandingPage($id) {
'#title' => $this->t('Personnel'),

];
$renderArray['personnel_wrapper']['personnel_tabs'] = \Drupal::formBuilder()->getForm('Drupal\metsis_lib\Form\PersonnelForm', $fields);
$renderArray['personnel_wrapper']['personnel_tabs'] = $this->formBuilder()->getForm('Drupal\metsis_lib\Form\PersonnelForm', $fields);

/*
* KEYWORDS
Expand All @@ -751,7 +753,7 @@ public function getLandingPage($id) {

];

$renderArray['keywords_wrapper']['keywords_tabs'] = \Drupal::formBuilder()->getForm('Drupal\metsis_lib\Form\KeywordsForm', $fields);
$renderArray['keywords_wrapper']['keywords_tabs'] = $this->formBuilder()->getForm('Drupal\metsis_lib\Form\KeywordsForm', $fields);

/*
* Platform and Instrument
Expand All @@ -762,7 +764,7 @@ public function getLandingPage($id) {
'#title' => $this->t('Aquisition Information'),

];
$renderArray['aquisition_wrapper']['aquisition_tabs'] = \Drupal::formBuilder()->getForm('Drupal\metsis_lib\Form\AquisitionForm', $fields);
$renderArray['aquisition_wrapper']['aquisition_tabs'] = $this->formBuilder()->getForm('Drupal\metsis_lib\Form\AquisitionForm', $fields);
}

$renderArray['metadata_update_wrapper'] = [
Expand All @@ -787,19 +789,9 @@ public function getLandingPage($id) {
'#title' => $this->t('Storage Information'),

];
$renderArray['storage_information_wrapper']['information'] = \Drupal::formBuilder()->getForm('Drupal\metsis_lib\Form\StorageInformationForm', $fields);

}
// $renderArray['#group_children']['temporal'] = 'extent';
// $renderArray['#group_children']['geographical'] = 'extent';
// $renderArray['extent']['tabs']['temporal']['temp_tab']['#tree'] = true;
// $renderArray['extent']['tabs']['geographical']['geo_tab']['#tree'] = true;
// $renderArray['extent']['tabs']['temporal']['temp_tab']['#parents'] = ['extent', 'tabs', 'temporal', 'temp_tab'];
// $renderArray['extent']['tabs']['geographical']['geo_tab']['#parents'] = ['extent', 'tabs', 'geographical', 'geo_tab'];
// $renderArray['#fieldgroups']['extent']->children[] = 'geographical';
// $renderArray['#attached']['library'][] = 'field_group/core';
// $renderArray['#attached']['library'][] = 'field_group/tabs';
// $renderArray['#attached']['library'][] = 'field_group/formatter.horizontal_tabs';.
$renderArray['storage_information_wrapper']['information'] = $this->formBuilder()->getForm('Drupal\metsis_lib\Form\StorageInformationForm', $fields);

}
$renderArray['#attached']['library'][] = 'metsis_lib/landing_page';
$renderArray['#attached']['library'][] = 'metsis_lib/fa_academia';
$renderArray['#cache']['max-age'] = 0;
Expand Down Expand Up @@ -892,53 +884,9 @@ public function getJsonld($fields, $host) {
'@type' => 'DataCatalog',
'name:' => $host,
],
'temporalCoverage' => $start_date . '/' . $start_date ?? '',
'temporalCoverage' => $start_date . '/' . $end_date ?? '',

];
$string = <<<EOF
"creator":{
"@type":"Organization",
"url": "https://www.ncei.noaa.gov/",
"name":"OC/NOAA/NESDIS/NCEI > National Centers for Environmental Information, NESDIS, NOAA, U.S. Department of Commerce",
"contactPoint":{
"@type":"ContactPoint",
"contactType": "customer service",
"telephone":"+1-828-271-4800",
"email":"[email protected]"
}
},
"funder":{
"@type": "Organization",
"sameAs": "https://ror.org/00tgqzw13",
"name": "National Weather Service"
},
"includedInDataCatalog":{
"@type":"DataCatalog",
"name":"adc.met.no"
},
"distribution":[
{
"@type":"DataDownload",
"encodingFormat":"CSV",
"contentUrl":"http://www.ncdc.noaa.gov/stormevents/ftp.jsp"
},
{
"@type":"DataDownload",
"encodingFormat":"XML",
"contentUrl":"http://gis.ncdc.noaa.gov/all-records/catalog/search/resource/details.page?id=gov.noaa.ncdc:C00510"
}
],
"temporalCoverage":"1950-01-01/2013-12-18",
"spatialCoverage":{
"@type":"Place",
"geo":{
"@type":"GeoShape",
"box":"18.0 -65.0 72.0 172.0"
}
}
}
EOF;
return $json;
}

Expand Down
18 changes: 1 addition & 17 deletions metsis/metsis_lib/src/Form/AquisitionForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Drupal\Core\Form\FormStateInterface;

/**
* Class AquisitionForm.
* Form for displaying platform and instruments in vertical tab.
*/
class AquisitionForm extends FormBase {

Expand Down Expand Up @@ -62,26 +62,10 @@ public function buildForm(array $form, FormStateInterface $form_state, $fields =
return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state->getValues() as $key => $value) {
// @todo Validate fields.
}
parent::validateForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
/* foreach ($form_state->getValues() as $key => $value) {
\Drupal::messenger()->addMessage($key . ': ' . ($key === 'text_format'?$value['value']:$value));
}
*/

}

}
12 changes: 0 additions & 12 deletions metsis/metsis_lib/src/Form/ExportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function buildForm(array $form, FormStateInterface $form_state, $fields =

// $selectedExports = $this->exportConfig->get('export_metadata');
// $this->logger('landing')->debug($selectedExports);

$form['export'] = [
'#type' => 'actions',
// '#tree' => true,.
Expand Down Expand Up @@ -96,16 +95,6 @@ public function buildForm(array $form, FormStateInterface $form_state, $fields =
return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state->getValues() as $key => $value) {
// @todo Validate fields.
}
parent::validateForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
Expand All @@ -119,7 +108,6 @@ public function ajaxCallback(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
/* foreach ($form_state->getValues() as $key => $value) {
\Drupal::messenger()->addMessage($key . ': ' . ($key === 'text_format'?$value['value']:$value));
} */
$mmd = $form_state->get('mmd');
$id = $form_state->get('id');
Expand Down
12 changes: 1 addition & 11 deletions metsis/metsis_lib/src/Form/ExtentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Drupal\Core\Form\FormStateInterface;

/**
* Class ExtentForm.
* CForm for displaying temporal and geographic extent in horizontal tabs.
*/
class ExtentForm extends FormBase {

Expand Down Expand Up @@ -99,16 +99,6 @@ public function buildForm(array $form, FormStateInterface $form_state, $fields =
return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state->getValues() as $key => $value) {
// @todo Validate fields.
}
parent::validateForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 2 additions & 12 deletions metsis/metsis_lib/src/Form/KeywordsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Drupal\Core\Form\FormStateInterface;

/**
* Class KeywordsForm.
* Display differnt keyword classes in horizontal tabs.
*/
class KeywordsForm extends FormBase {

Expand Down Expand Up @@ -68,7 +68,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $fields =

$form[$vocab_tag] = [
'#type' => 'details',
'#title' => $this->t($vocab),
'#title' => $this->t('@vocab', ['@bocab' => $vocab]),
'#weight' => '0',
'#group' => 'keywords',
'#attributes' => [
Expand Down Expand Up @@ -272,16 +272,6 @@ public function buildForm(array $form, FormStateInterface $form_state, $fields =
return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state->getValues() as $key => $value) {
// @todo Validate fields.
}
parent::validateForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
Expand Down
11 changes: 0 additions & 11 deletions metsis/metsis_lib/src/Form/MetsisLibConfigurationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*
* NOTE: Implement form validation here.
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Get user and pass from admin configuration.
$values = $form_state->getValues();

}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 2 additions & 12 deletions metsis/metsis_lib/src/Form/PersonnelForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Drupal\Core\Form\FormStateInterface;

/**
* Class PersonnelForm.
* Show the diferrnet personnele in a horizontal tab.
*/
class PersonnelForm extends FormBase {

Expand All @@ -30,7 +30,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $fields =
$role_tag = str_replace(' ', '_', strtolower($role));
$form[$role_tag] = [
'#type' => 'details',
'#title' => $this->t($role),
'#title' => $this->t('@role', ['@role' => $role]),
'#weight' => '0',
'#group' => 'personnel',
'#attributes' => [
Expand Down Expand Up @@ -213,16 +213,6 @@ public function buildForm(array $form, FormStateInterface $form_state, $fields =
return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state->getValues() as $key => $value) {
// @todo Validate fields.
}
parent::validateForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
Expand Down
Loading

0 comments on commit 87d73bb

Please sign in to comment.