Skip to content

Commit

Permalink
Merge pull request #293 from UN-OCHA/berliner/HPC-9973
Browse files Browse the repository at this point in the history
HPC-9973: Wrap tags and prevent publishing with no tags
  • Loading branch information
berliner authored Dec 12, 2024
2 parents 349198e + 9976606 commit 95c1376
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 21 deletions.
36 changes: 26 additions & 10 deletions config/core.entity_form_display.node.article.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -177,7 +193,7 @@ content:
third_party_settings: { }
field_country:
type: select_active_tags
weight: 5
weight: 6
region: content
settings:
match_operator: CONTAINS
Expand All @@ -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
Expand All @@ -204,7 +220,7 @@ content:
third_party_settings: { }
field_month:
type: select_active_tags
weight: 4
weight: 5
region: content
settings:
match_operator: CONTAINS
Expand Down Expand Up @@ -256,7 +272,7 @@ content:
hide_guidelines: '1'
field_theme:
type: select_active_tags
weight: 6
weight: 7
region: content
settings:
match_operator: CONTAINS
Expand All @@ -266,7 +282,7 @@ content:
third_party_settings: { }
field_year:
type: select_active_tags
weight: 3
weight: 4
region: content
settings:
match_operator: CONTAINS
Expand Down
36 changes: 26 additions & 10 deletions config/core.entity_form_display.node.document.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -139,7 +155,7 @@ content:
third_party_settings: { }
field_country:
type: select_active_tags
weight: 31
weight: 32
region: content
settings:
match_operator: CONTAINS
Expand All @@ -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
Expand All @@ -166,7 +182,7 @@ content:
third_party_settings: { }
field_month:
type: select_active_tags
weight: 30
weight: 31
region: content
settings:
match_operator: CONTAINS
Expand Down Expand Up @@ -208,7 +224,7 @@ content:
third_party_settings: { }
field_theme:
type: select_active_tags
weight: 32
weight: 33
region: content
settings:
match_operator: CONTAINS
Expand All @@ -218,7 +234,7 @@ content:
third_party_settings: { }
field_year:
type: select_active_tags
weight: 29
weight: 30
region: content
settings:
match_operator: CONTAINS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public function getBundleLabel() {
return $this->type->entity->label();
}

/**
* {@inheritdoc}
*/
public function hasTags() {
return !$this->get('field_computed_tags')->isEmpty();
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions html/modules/custom/ncms_ui/src/Entity/ContentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Drupal\ncms_ui\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Form class for an alert form if no tags have been added yet.
*/
class ContentSubmitNoTagsAlertForm extends ConfirmFormBase {

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->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('<front>');
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Empty stub just because FormInterface requires it.
}

}

0 comments on commit 95c1376

Please sign in to comment.