Skip to content
Snippets Groups Projects
Commit dbb5c90e authored by Aleix Quintana's avatar Aleix Quintana
Browse files

fix #3482327: Move default content to a submodule. Refactoring default import...

fix #3482327: Move default content to a submodule. Refactoring default import logics to let user choose
parent 44b9e9c2
No related branches found
No related tags found
No related merge requests found
......@@ -43,12 +43,3 @@ function licenses_vocabulary_help($route_name, RouteMatchInterface $route_match)
}
}
/**
* Implements hook_modules_installed().
*
* Add default licenses.
*/
function licenses_vocabulary_modules_installed($modules) {
// Load default license terms from the module config.
\Drupal::service('licenses_vocabulary.manager')->loadDefaultLicenses();
}
......@@ -21,7 +21,7 @@ class LicensesImportForm extends ConfigFormBase {
*
* @var \Drupal\licenses_vocabulary\LicensesManagerInterface
*/
protected $LicensesManager;
protected $licensesManager;
/**
* The Messenger service.
......@@ -85,9 +85,9 @@ class LicensesImportForm extends ConfigFormBase {
$info = $this->t('This module adds license as taxonomy terms.');
$info .= "<br/>";
$info .= $this->t('It will import all licenses from @file', ['@file' => 'vocabulary_licenses/licenses_vocabulary.services.yml']);
$info .= $this->t('Submitting this form will import the licenses specified in below text area.');
$info .= "<br/>";
$info .= $this->t('Also will import the licenses specified in text area from below');
$info .= $this->t('Additionally it will import all licenses from @file.', ['@file' => 'vocabulary_licenses/licenses_vocabulary.services.yml']);
$info .= "<br/>";
$tax_url = Link::fromTextAndUrl("taxonomy settings", Url::fromUri("internal:/admin/structure/taxonomy/manage/licenses_vocabulary_licenses/overview"))->toString();
$info .= $this->t('Press save and it will start the import, then go to @url', ['@url' => $tax_url]);
......@@ -121,6 +121,23 @@ class LicensesImportForm extends ConfigFormBase {
'#prefix' => '<pre>',
'#suffix' => '</pre>',
];
$default_licenses = $this->licensesManager->listDefaultLicenses();
$form['import_default_list'] = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#title' => t('Default licenses:'),
];
foreach($default_licenses as $default_license) {
$form['import_default_list']['#items'][] = $default_license['title'];
}
$form['import_default'] = [
'#type' => $this->t('Import default licenses'),
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => t('Import default licenses'),
'#description' => $this->t('Import also next license list from @file.', ['@file' => 'vocabulary_licenses/licenses_vocabulary.services.yml']),
];
return $form;
}
......@@ -130,23 +147,25 @@ class LicensesImportForm extends ConfigFormBase {
public function validateForm(array &$form, FormStateInterface $form_state) {
$licenses = [];
if ($form_state->getValue('licenses_text')) {
$licenses = $this->licensesManager->loadFromText($form_state->getValue('licenses_text'));
$licenses = $this->licensesManager->createFromText($form_state->getValue('licenses_text'));
}
if ($form_state->getValue('import_default')) {
$licenses = $licenses + $this->licensesManager->createDefaultLicenses();
}
$licenses = $licenses + $this->licensesManager->loadDefaultLicenses();
if (count($licenses) == 0) {
$form_state->setErrorByName('licenses_text', $this->t("No license imported."));
$form_state->setErrorByName('licenses_text', $this->t("No license to import."));
}
foreach ($licenses as $license) {
if (array_key_exists(LICENSES_VOCABULARY_WRONG_TEXT, $license)) {
if (is_array($license) && array_key_exists(LICENSES_VOCABULARY_WRONG_TEXT, $license)) {
$form_state->setErrorByName('licenses_text', $license[LICENSES_VOCABULARY_WRONG_TEXT]);
}
elseif (array_key_exists(LICENSES_VOCABULARY_FILE_NOT_FOUND, $license)) {
elseif (is_array($license) && array_key_exists(LICENSES_VOCABULARY_FILE_NOT_FOUND, $license)) {
$form_state->setErrorByName('licenses_text', $license[LICENSES_VOCABULARY_FILE_NOT_FOUND]);
}
elseif (array_key_exists(LICENSES_VOCABULARY_TERM_NOT_CREATED, $license)) {
elseif (is_array($license) && array_key_exists(LICENSES_VOCABULARY_TERM_NOT_CREATED, $license)) {
$form_state->setErrorByName('licenses_text', $license[LICENSES_VOCABULARY_TERM_NOT_CREATED]);
}
elseif (array_key_exists(LICENSES_VOCABULARY_TERM_EXISTS, $license)) {
elseif (is_array($license) && array_key_exists(LICENSES_VOCABULARY_TERM_EXISTS, $license)) {
$this->messenger->addStatus($license[LICENSES_VOCABULARY_TERM_EXISTS]);
}
else {
......
......@@ -47,31 +47,53 @@ class LicensesManager implements LicensesManagerInterface {
*/
public function loadFromText($text) {
$license_data = Yaml::decode($text);
$newLicenses = [];
$licenses = [];
if (is_array($license_data)) {
foreach ($license_data as $license_item) {
$keys = ['title', 'short_label', 'icon_file', 'url'];
if (count(array_diff($keys, array_keys($license_item))) > 0) {
return [LICENSES_VOCABULARY_WRONG_TEXT => $this->t('The license could not be created, wrong text')];
}
$license_term = $this->createLicenseTerm($license_item['title'], $license_item['short_label'], $license_item['icon_file'], $license_item['url']);
if ($license_term) {
$newLicenses[] = $license_term;
return [LICENSES_VOCABULARY_WRONG_TEXT => $this->t('Some license could not be listed, wrong text')];
}
$licenses[] = $license_item;
}
}
return $licenses;
}
/**
* {@inheritdoc}
*/
public function createFromText($text) {
$license_data = $this->loadFromText($text);
$newLicenses = [];
foreach ($license_data as $license_item) {
$license_term = $this->createLicenseTerm($license_item['title'], $license_item['short_label'], $license_item['icon_file'], $license_item['url']);
if ($license_term) {
$newLicenses[] = $license_term;
}
}
return $newLicenses;
}
/**
* {@inheritdoc}
*/
public function loadDefaultLicenses() {
public function listDefaultLicenses() {
$file_path = \Drupal::service('extension.list.module')->getPath('licenses_vocabulary') . '/licenses_vocabulary.default.licenses.yml';
$file_contents = file_get_contents($file_path);
return $this->loadFromText($file_contents);
}
/**
* {@inheritdoc}
*/
public function createDefaultLicenses() {
$file_path = \Drupal::service('extension.list.module')->getPath('licenses_vocabulary') . '/licenses_vocabulary.default.licenses.yml';
$file_contents = file_get_contents($file_path);
return $this->createFromText($file_contents);
}
/**
* Creates the license if it doesn't already exist.
*
......
......@@ -11,9 +11,9 @@ interface LicensesManagerInterface {
* Read the list of pre-defined licenses and create each taxonomy terms.
*
* @return array
* Array with the multiple Drupal\taxonomy\TermInterface it can create.
* Array with the multiple Drupal\taxonomy\TermInterface that can be created.
*/
public function loadDefaultLicenses();
public function createDefaultLicenses();
/**
* Parse text to import licenses from it.
......@@ -22,7 +22,26 @@ interface LicensesManagerInterface {
* The text in YML to import.
*
* @return array
* Array with the multiple Drupal\taxonomy\TermInterface it can create.
* Array with the multiple Drupal\taxonomy\TermInterface that can be created.
*/
public function createFromText($text);
/**
* Read the list of pre-defined licenses.
*
* @return array
* Array with the multiple default licenses that can be get.
*/
public function listDefaultLicenses();
/**
* Parse text to get licenses from it.
*
* @param string $text
* The text in YML to get.
*
* @return array
* Array with the multiple licenses that can be get.
*/
public function loadFromText($text);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment