Skip to content

Commit

Permalink
Merge pull request #862 from City-of-Helsinki/UHF-10539
Browse files Browse the repository at this point in the history
UHF-10539: Unpublishable redirects
  • Loading branch information
hyrsky authored Dec 10, 2024
2 parents 392d323 + ee72b4b commit ba094b3
Show file tree
Hide file tree
Showing 16 changed files with 1,395 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This repository holds configuration for the Hel.fi platform.
- [Update instructions (2.x to 3.x)](documentation/update.md)
- [Two-factor authentication/TFA/MFA](/modules/helfi_tfa/README.md)
- [JSON:API remote entities](/modules/helfi_etusivu_entities/README.md)
- [Redirects](/documentation/redirects.md)
- [Users](/modules/helfi_users/README.md)

## Contact
Expand Down
6 changes: 6 additions & 0 deletions config/schema/helfi_platform_config.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ block.settings.telia_ace_widget:
type: text
label: 'Chat button label'

helfi_platform_config.redirect_cleaner:
type: config_object
label: 'Redirect cleaner'
mapping:
enable:
type: boolean

# Default value.
field.value.location:
Expand Down
21 changes: 21 additions & 0 deletions documentation/redirects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Publishable redirects

This module alters [redirect entity type](../src/Entity/PublishableRedirect.php) from
[`redirect` module](https://www.drupal.org/project/redirect)
so that it implements EntityPublishedInterface and has
`is_custom` field.

The redirect entities added or updated by any user from through the
entity from are automatically permanently marked as custom, while
redirects created automatically are not custom.

If enabled, a cron job unpublished non-custom redirect entities that
are more than 6 months old. Enable the feature with:

```php
\Drupal::configFactory()
->getEditable('helfi_platform_config.redirect_cleaner')
->set('enable', TRUE)
->save();
```

72 changes: 72 additions & 0 deletions helfi_platform_config.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
* Contains installation hooks for HELfi platform config module.
*/

declare(strict_types=1);

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\helfi_api_base\Environment\ActiveProjectRoles;
use Drupal\helfi_api_base\Environment\ProjectRoleEnum;
use Drupal\helfi_platform_config\Entity\PublishableRedirect;
use Drupal\user\Entity\User;

/**
Expand Down Expand Up @@ -229,3 +235,69 @@ function helfi_platform_config_update_9315(): void {
$config_factory->getEditable('core.entity_view_display.media.image.image_content_area')
->delete();
}

/**
* UHF-10539: Update redirect entity type.
*/
function helfi_platform_config_update_9316() : void {
if (!\Drupal::moduleHandler()->moduleExists('redirect')) {
return;
}

$updateManager = \Drupal::entityDefinitionUpdateManager();
$entityTypes = [
'redirect' => $updateManager->getEntityType('redirect'),
];

helfi_platform_config_entity_type_build($entityTypes);

/** @var \Drupal\Core\Entity\EntityTypeInterface $entityType */
$entityType = reset($entityTypes);

$fields = PublishableRedirect::baseFieldDefinitions($entityType);

// Revert class change.
$entityType->setClass($entityType->getOriginalClass());

// Update entity settings without updating the class.
$updateManager->updateEntityType($entityType);

foreach (['published', 'custom'] as $key) {
$field = $fields[$entityType->getKey($key)];

// Set published and custom initially to TRUE.
if ($field instanceof BaseFieldDefinition) {
$field->setInitialValue(TRUE);
}

$updateManager->installFieldStorageDefinition(
$entityType->getKey($key),
$entityType->id(),
'helfi_platform_config',
$field
);
}

helfi_platform_config_entity_type_build($entityTypes);

/** @var \Drupal\Core\Entity\EntityTypeInterface $entityType */
$entityType = reset($entityTypes);

// Update entity type again with the class change.
$updateManager->updateEntityType($entityType);
}

/**
* UHF-10539: Update redirect entity type.
*/
function helfi_platform_config_update_9317() : void {
/** @var \Drupal\helfi_api_base\Environment\ActiveProjectRoles $projectRoles */
$projectRoles = \Drupal::service(ActiveProjectRoles::class);

if ($projectRoles->hasRole(ProjectRoleEnum::Core)) {
\Drupal::configFactory()
->getEditable('helfi_platform_config.redirect_cleaner')
->set('enable', TRUE)
->save();
}
}
53 changes: 53 additions & 0 deletions helfi_platform_config.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,40 @@ declare(strict_types=1);
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldConfigBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\block\Entity\Block;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\helfi_platform_config\DTO\ParagraphTypeCollection;
use Drupal\helfi_platform_config\EntityVersionMatcher;
use Drupal\helfi_platform_config\Entity\PublishableRedirect;
use Drupal\helfi_platform_config\RedirectCleaner;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\user\Entity\Role;

/**
* Implements hook_entity_type_build().
*/
function helfi_platform_config_entity_type_build(array &$entity_types): void {
if (isset($entity_types['redirect'])) {
$entity_types['redirect']->setClass(PublishableRedirect::class);
$entity_types['redirect']->set('entity_keys', $entity_types['redirect']->getKeys() + [
'published' => 'status',
'custom' => 'is_custom',
]);
}
}

/**
* Implements hook_modules_installed().
*/
Expand Down Expand Up @@ -505,3 +523,38 @@ function helfi_platform_config_config_schema_info_alter(array &$definitions) : v
$definitions['social_media.item.email']['mapping']['text']['translation context'] = 'Social media: email';
}
}

/**
* Implements hook_form_alter().
*/
function helfi_platform_config_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
if (in_array($form_id, ['redirect_redirect_edit_form', 'redirect_redirect_form'])) {
// Set is_custom field to true whenever redirect entity is saved from
// entity form. The field defaults to FALSE if it is saved from Drupal API.
$form['is_custom'] = [
'#type' => 'hidden',
'#access' => FALSE,
'#default_value' => [TRUE],
];

$formObject = $form_state->getFormObject();
assert($formObject instanceof EntityFormInterface);
$redirect = $formObject->getEntity();
if ($redirect instanceof EntityPublishedInterface) {
$form['status'] = [
'#type' => 'checkbox',
'#title' => new TranslatableMarkup('Published'),
'#default_value' => $redirect->isPublished(),
];
};
}
}

/**
* Implements hook_cron().
*/
function helfi_platform_config_cron(): void {
/** @var \Drupal\helfi_platform_config\RedirectCleaner $cleaner */
$cleaner = \Drupal::service(RedirectCleaner::class);
$cleaner->unpublishExpiredRedirects();
}
3 changes: 3 additions & 0 deletions helfi_platform_config.services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
services:
_defaults:
autoconfigure: true
autowire: true

Drupal\helfi_platform_config\Helper\BlockInstaller: '@helfi_platform_config.helper.block_installer'
helfi_platform_config.helper.block_installer:
Expand Down Expand Up @@ -79,3 +80,5 @@ services:
- '@file_url_generator'
tags:
- { name: helfi_platform_config.og_image_builder, priority: -100 }

Drupal\helfi_platform_config\RedirectCleaner: ~
Loading

0 comments on commit ba094b3

Please sign in to comment.