From 9976606c43f90404c96c1e09f1197af69bbead44 Mon Sep 17 00:00:00 2001 From: berliner Date: Thu, 12 Dec 2024 13:17:04 +0100 Subject: [PATCH] HPC-9973: Wrap tags and prevent publishing with no tags --- ...tity_form_display.node.article.default.yml | 36 +++++-- ...ity_form_display.node.document.default.yml | 36 +++++-- .../src/Entity/Content/ContentBase.php | 7 ++ .../ncms_ui/src/Entity/ContentInterface.php | 8 ++ .../ncms_ui/src/Form/ContentBaseFormAlter.php | 9 +- .../src/Form/ContentSubmitNoTagsAlertForm.php | 93 +++++++++++++++++++ 6 files changed, 168 insertions(+), 21 deletions(-) create mode 100644 html/modules/custom/ncms_ui/src/Form/ContentSubmitNoTagsAlertForm.php diff --git a/config/core.entity_form_display.node.article.default.yml b/config/core.entity_form_display.node.article.default.yml index 6f8802da..097ea2ec 100755 --- a/config/core.entity_form_display.node.article.default.yml +++ b/config/core.entity_form_display.node.article.default.yml @@ -58,11 +58,7 @@ third_party_settings: group_meta_data: children: - field_content_space - - field_document_type - - field_year - - field_month - - field_country - - field_theme + - group_tags - field_author - field_pdf label: Properties @@ -113,6 +109,26 @@ third_party_settings: formatter: closed description: '' required_fields: true + group_tags: + children: + - field_document_type + - field_year + - field_month + - field_country + - field_theme + label: Tags + region: content + parent_name: group_meta_data + weight: 2 + format_type: details + format_settings: + classes: '' + show_empty_fields: false + id: '' + label_as_html: false + open: true + description: '' + required_fields: true id: node.article.default targetEntityType: node bundle: article @@ -177,7 +193,7 @@ content: third_party_settings: { } field_country: type: select_active_tags - weight: 5 + weight: 6 region: content settings: match_operator: CONTAINS @@ -187,7 +203,7 @@ content: third_party_settings: { } field_document_type: type: select_active_tags - weight: 2 + weight: 3 region: content settings: match_operator: CONTAINS @@ -204,7 +220,7 @@ content: third_party_settings: { } field_month: type: select_active_tags - weight: 4 + weight: 5 region: content settings: match_operator: CONTAINS @@ -256,7 +272,7 @@ content: hide_guidelines: '1' field_theme: type: select_active_tags - weight: 6 + weight: 7 region: content settings: match_operator: CONTAINS @@ -266,7 +282,7 @@ content: third_party_settings: { } field_year: type: select_active_tags - weight: 3 + weight: 4 region: content settings: match_operator: CONTAINS diff --git a/config/core.entity_form_display.node.document.default.yml b/config/core.entity_form_display.node.document.default.yml index e5b8981a..20c239c9 100644 --- a/config/core.entity_form_display.node.document.default.yml +++ b/config/core.entity_form_display.node.document.default.yml @@ -63,11 +63,7 @@ third_party_settings: group_meta_data: children: - field_content_space - - field_document_type - - field_year - - field_month - - field_country - - field_theme + - group_tags - field_summary label: Properties region: content @@ -97,6 +93,26 @@ third_party_settings: formatter: closed description: '' required_fields: true + group_tags: + children: + - field_document_type + - field_year + - field_month + - field_country + - field_theme + label: Tags + region: content + parent_name: group_meta_data + weight: 28 + format_type: details + format_settings: + classes: '' + show_empty_fields: false + id: '' + label_as_html: false + open: true + description: '' + required_fields: true id: node.document.default targetEntityType: node bundle: document @@ -139,7 +155,7 @@ content: third_party_settings: { } field_country: type: select_active_tags - weight: 31 + weight: 32 region: content settings: match_operator: CONTAINS @@ -149,7 +165,7 @@ content: third_party_settings: { } field_document_type: type: select_active_tags - weight: 28 + weight: 29 region: content settings: match_operator: CONTAINS @@ -166,7 +182,7 @@ content: third_party_settings: { } field_month: type: select_active_tags - weight: 30 + weight: 31 region: content settings: match_operator: CONTAINS @@ -208,7 +224,7 @@ content: third_party_settings: { } field_theme: type: select_active_tags - weight: 32 + weight: 33 region: content settings: match_operator: CONTAINS @@ -218,7 +234,7 @@ content: third_party_settings: { } field_year: type: select_active_tags - weight: 29 + weight: 30 region: content settings: match_operator: CONTAINS diff --git a/html/modules/custom/ncms_ui/src/Entity/Content/ContentBase.php b/html/modules/custom/ncms_ui/src/Entity/Content/ContentBase.php index ba4cf1d1..8fbb57e2 100644 --- a/html/modules/custom/ncms_ui/src/Entity/Content/ContentBase.php +++ b/html/modules/custom/ncms_ui/src/Entity/Content/ContentBase.php @@ -82,6 +82,13 @@ public function getBundleLabel() { return $this->type->entity->label(); } + /** + * {@inheritdoc} + */ + public function hasTags() { + return !$this->get('field_computed_tags')->isEmpty(); + } + /** * {@inheritdoc} */ diff --git a/html/modules/custom/ncms_ui/src/Entity/ContentInterface.php b/html/modules/custom/ncms_ui/src/Entity/ContentInterface.php index 0fa00c2a..07152eae 100644 --- a/html/modules/custom/ncms_ui/src/Entity/ContentInterface.php +++ b/html/modules/custom/ncms_ui/src/Entity/ContentInterface.php @@ -25,6 +25,14 @@ public function getOverviewUrl(); */ public function getBundleLabel(); + /** + * Check if the content has any tags. + * + * @return bool + * TRUE if it can be published, FALSE otherwise. + */ + public function hasTags(); + /** * Mark this entity as deleted. */ diff --git a/html/modules/custom/ncms_ui/src/Form/ContentBaseFormAlter.php b/html/modules/custom/ncms_ui/src/Form/ContentBaseFormAlter.php index 1f320051..63b27bde 100644 --- a/html/modules/custom/ncms_ui/src/Form/ContentBaseFormAlter.php +++ b/html/modules/custom/ncms_ui/src/Form/ContentBaseFormAlter.php @@ -249,10 +249,17 @@ public function ajaxConfirm(array &$form, FormStateInterface $form_state) { $entity_updated = $this->entityCompare->hasChanged($updated_entity, $original_entity); $publish_actions = [ + 'save_and_publish', 'publish_correction', 'publish_revision', ]; - if ($original_entity->isPublished() && !$entity_updated && in_array($triggering_element['#name'], $publish_actions)) { + if (in_array($triggering_element['#name'], $publish_actions) && !$updated_entity->hasTags()) { + $content = $this->formBuilder->getForm(ContentSubmitNoTagsAlertForm::class, $triggering_element, $original_entity); + $response->addCommand(new OpenModalDialogCommand($this->t('Publishing not possible'), $content, [ + 'width' => '60vw', + ])); + } + elseif ($original_entity->isPublished() && !$entity_updated && in_array($triggering_element['#name'], $publish_actions)) { $content = $this->formBuilder->getForm(ContentSubmitNoChangesAlertForm::class, $triggering_element, $original_entity); $response->addCommand(new OpenModalDialogCommand($this->t('Confirmation'), $content, [ 'width' => '60vw', diff --git a/html/modules/custom/ncms_ui/src/Form/ContentSubmitNoTagsAlertForm.php b/html/modules/custom/ncms_ui/src/Form/ContentSubmitNoTagsAlertForm.php new file mode 100644 index 00000000..7752e803 --- /dev/null +++ b/html/modules/custom/ncms_ui/src/Form/ContentSubmitNoTagsAlertForm.php @@ -0,0 +1,93 @@ +entityTypeManager = $container->get('entity_type.manager'); + return $instance; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'content_submit_no_tags_alert_form'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + $bundle = $this->getRouteMatch()->getRawParameter('node_type') ?? $this->getRouteMatch()->getParameter('node')->bundle(); + $node_type = $this->entityTypeManager->getStorage('node_type')->load($bundle) ?? $this->t('entity'); + $node_type_label = strtolower($node_type->label()); + if (in_array($node_type_label[0], ['a', 'e', 'i', 'o', 'u'])) { + return $this->t('An @type cannot be published without any tags associated with it. Please add at least one tag and try again, or save the @type as a draft instead', [ + '@type' => $node_type_label, + ]); + } + else { + return $this->t('A @type cannot be published without any tags associated with it. Please add at least one tag and try again, or save the @type as a draft instead', [ + '@type' => $node_type_label, + ]); + } + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Ok'); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $triggering_element = NULL, $entity = NULL) { + $form = parent::buildForm($form, $form_state); + + $form['description'] = ['#markup' => $this->getQuestion()]; + $form['actions']['submit']['#access'] = FALSE; + // This is a special class to which JavaScript assigns dialog closing + // behavior. + $form['actions']['cancel']['#attributes']['class'][] = 'dialog-cancel'; + $form['actions']['cancel']['#title'] = $this->getConfirmText(); + return $form; + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return Url::fromRoute(''); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // Empty stub just because FormInterface requires it. + } + +}