Skip to content
Snippets Groups Projects
Commit 63d81711 authored by Brian Canini's avatar Brian Canini
Browse files

Merge branch 'lee5151' into 'main'

Upgrading drupal/allowed_formats (2.0.0 => 3.0.0)

See merge request !89
parents 9425655b d0437325
No related branches found
No related tags found
1 merge request!89Upgrading drupal/allowed_formats (2.0.0 => 3.0.0)
Showing
with 207 additions and 369 deletions
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "fed9e5973d9822ac542407b5f14d599d",
"content-hash": "b9ceb162827c3271a903ef5eb6c1e303",
"packages": [
{
"name": "asm89/stack-cors",
......@@ -1523,26 +1523,29 @@
},
{
"name": "drupal/allowed_formats",
"version": "2.0.0",
"version": "3.0.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/allowed_formats.git",
"reference": "2.0.0"
"reference": "3.0.0"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/allowed_formats-2.0.0.zip",
"reference": "2.0.0",
"shasum": "ac6c6d398f303608ced7e9cd9d4556a728dc41f0"
"url": "https://ftp.drupal.org/files/projects/allowed_formats-3.0.0.zip",
"reference": "3.0.0",
"shasum": "1dad855db0e12fa3cdef8ca4e3bfc98f89090490"
},
"require": {
"drupal/core": "^9.2 || ^10"
"drupal/core": "^10.1"
},
"conflict": {
"drupal/core": "<10.1.0"
},
"type": "drupal-module",
"extra": {
"drupal": {
"version": "2.0.0",
"datestamp": "1669170410",
"version": "3.0.0",
"datestamp": "1693983469",
"security-coverage": {
"status": "covered",
"message": "Covered by Drupal's security advisory policy"
......@@ -1575,7 +1578,7 @@
"role": "Maintainer"
}
],
"description": "Limit which text formats are available for each field instance.",
"description": "Hides info about the selected text format. The 'allowed formats' functionality has been moved to core since Drupal 10.1.0.",
"homepage": "https://www.drupal.org/project/allowed_formats",
"support": {
"source": "http://cgit.drupalcode.org/allowed_formats",
......
......@@ -1597,27 +1597,30 @@
},
{
"name": "drupal/allowed_formats",
"version": "2.0.0",
"version_normalized": "2.0.0.0",
"version": "3.0.0",
"version_normalized": "3.0.0.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/allowed_formats.git",
"reference": "2.0.0"
"reference": "3.0.0"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/allowed_formats-2.0.0.zip",
"reference": "2.0.0",
"shasum": "ac6c6d398f303608ced7e9cd9d4556a728dc41f0"
"url": "https://ftp.drupal.org/files/projects/allowed_formats-3.0.0.zip",
"reference": "3.0.0",
"shasum": "1dad855db0e12fa3cdef8ca4e3bfc98f89090490"
},
"require": {
"drupal/core": "^9.2 || ^10"
"drupal/core": "^10.1"
},
"conflict": {
"drupal/core": "<10.1.0"
},
"type": "drupal-module",
"extra": {
"drupal": {
"version": "2.0.0",
"datestamp": "1669170410",
"version": "3.0.0",
"datestamp": "1693983469",
"security-coverage": {
"status": "covered",
"message": "Covered by Drupal's security advisory policy"
......@@ -1651,7 +1654,7 @@
"role": "Maintainer"
}
],
"description": "Limit which text formats are available for each field instance.",
"description": "Hides info about the selected text format. The 'allowed formats' functionality has been moved to core since Drupal 10.1.0.",
"homepage": "https://www.drupal.org/project/allowed_formats",
"support": {
"source": "http://cgit.drupalcode.org/allowed_formats",
......
......@@ -3,7 +3,7 @@
'name' => 'osu-asc-webservices/d8-upstream',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '781497112f254b5d0d030d7604b24ba74e07b295',
'reference' => '9425655bedfe42a3671e0e52bb49f53135b5e2db',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
......@@ -254,9 +254,9 @@
'dev_requirement' => false,
),
'drupal/allowed_formats' => array(
'pretty_version' => '2.0.0',
'version' => '2.0.0.0',
'reference' => '2.0.0',
'pretty_version' => '3.0.0',
'version' => '3.0.0.0',
'reference' => '3.0.0',
'type' => 'drupal-module',
'install_path' => __DIR__ . '/../../web/modules/allowed_formats',
'aliases' => array(),
......@@ -1402,7 +1402,7 @@
'osu-asc-webservices/d8-upstream' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '781497112f254b5d0d030d7604b24ba74e07b295',
'reference' => '9425655bedfe42a3671e0e52bb49f53135b5e2db',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
......
CONTENTS OF THIS FILE
---------------------
* Introduction
* Requirements
* Installation
* Configuration
* Maintainers
INTRODUCTION
------------
The Allowed Formats module limits which text formats are available for each
field instance.
* For a full description of the module visit:
https://www.drupal.org/project/allowed_formats
* To submit bug reports and feature suggestions, or to track changes visit:
https://www.drupal.org/project/issues/allowed_formats
REQUIREMENTS
------------
This module requires no modules outside of Drupal core.
INSTALLATION
------------
* Install the Allowed Formats module as you would normally install a
contributed Drupal module. Visit https://www.drupal.org/node/1897420 for
further information.
CONFIGURATION
-------------
There is no real configuration necessary. Navigate to Administration >
Extend and enable the module.
There is now a set of checkboxes for field settings (not the widget settings on
the form display tab) of text fields with a list of allowed formats: Basic
HTML, Restricted HTML, Full HTML, and Plain text.
Note that base fields defined by the entity type (for example the description
field of taxonomy terms) cannot have their allowed formats limited through the
UI.
MAINTAINERS
-----------
* Jordan Thompson (nord102) - https://www.drupal.org/u/nord102
* Florian Loretan (floretan) - https://www.drupal.org/u/floretan
Supporting organization:
* Northern Commerce - https://www.drupal.org/northern-commerce
* Wunder - https://www.drupal.org/wunder-group
IMPORTANT CHANGES IN 3.x BRANCH
-------------------------------
* Since Drupal 10.1.0, limiting the text formats per field instance is a feature
provided by Drupal core. Read https://www.drupal.org/node/3318572 for details.
* In the 3.x branch of this module this feature has been removed as obsolete,
but the module provide an update path from existing sites to move the allowed
formats, as they were stored by the previous versions of the module, to Drupal
>=10.1.0 way, in field settings.
* The module provides also a feature that allows site builders to hide the
formatted text format help and guidelines. Even this feature is still
preserved in the 3.x module branch, there is an issue that aims to move it in
Drupal core in the future. See https://www.drupal.org/i/3323007.
name: Allowed Formats
description: Limit which text formats are available for each field instance.
name: Hide format info (former Allowed Formats)
description: Hides info about the selected text format. The 'allowed formats' functionality has been moved in core since Drupal 10.1.0.
type: module
core_version_requirement: ^9.2 || ^10
core_version_requirement: ^10.1
dependencies:
- drupal:field
- drupal:filter
# Information added by Drupal.org packaging script on 2022-11-23
version: '2.0.0'
# Information added by Drupal.org packaging script on 2023-09-06
version: '3.0.0'
project: 'allowed_formats'
datestamp: 1669170415
datestamp: 1693982789
......@@ -7,51 +7,10 @@
* It enables limiting which text formats are available for each fields.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldConfigBase;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
/**
* Implements hook_form_FORM_ID_form_alter().
*/
function allowed_formats_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Field\FieldConfigInterface $field */
$field = $form_state->getFormObject()->getEntity();
if (in_array($field->getType(), _allowed_formats_field_types())) {
$options = [];
foreach (filter_formats() as $format) {
$options[$format->id()] = $format->label();
}
$settings = $field->getThirdPartySettings('allowed_formats');
$form['third_party_settings']['allowed_formats']['allowed_formats'] = [
'#type' => 'checkboxes',
'#title' => t('Allowed formats'),
'#options' => $options,
'#default_value' => !empty($settings['allowed_formats']) ? $settings['allowed_formats'] : [],
'#description' => t('Restrict which text formats are allowed, given the user has the required permissions. If no text formats are selected, then all the ones the user has access to will be available.'),
];
$form['#validate'][] = 'allowed_formats_form_field_config_edit_form_validate';
}
}
/**
* Validation handler for field_config_edit_form.
*/
function allowed_formats_form_field_config_edit_form_validate(array &$form, FormStateInterface $form_state) {
// Restructure options to store the value as a sequence.
$form_parents = [
'third_party_settings',
'allowed_formats',
'allowed_formats',
];
$value = $form_state->getValue($form_parents);
$form_state->setValue($form_parents, array_keys(array_filter($value)));
}
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\FieldConfigInterface;
/**
* Implements hook_field_widget_third_party_settings_form().
......@@ -85,23 +44,6 @@ function allowed_formats_field_widget_single_element_form_alter(&$element, FormS
$is_default_value_widget = (bool) $form_state->get('default_value_widget');
if (in_array($field_definition->getType(), _allowed_formats_field_types()) && !$is_default_value_widget) {
if ($field_definition instanceof FieldConfigBase) {
// Read configuration if available. This is possible for bundle fields or
// base fields overridden using a BaseFieldOverride.
$field_configuration = $field_definition->getConfig($field_definition->getTargetBundle());
$allowed_formats_setting = $field_configuration->getThirdPartySetting('allowed_formats', 'allowed_formats');
}
else {
// Base fields don't support third party settings so use an ordinary
// setting. That's the way it would work anyway if allowed formats gets
// into core.
$allowed_formats_setting = $field_definition->getSetting('allowed_formats');
}
if (!empty($allowed_formats_setting) && is_array($allowed_formats_setting)) {
$element['#allowed_formats'] = $allowed_formats_setting;
}
/** @var \Drupal\Core\Field\WidgetInterface $widget */
$widget = $context['widget'];
$element['#allowed_format_hide_settings'] = $widget->getThirdPartySettings('allowed_formats');
......@@ -146,3 +88,52 @@ function _allowed_formats_remove_textarea_help($form_element, FormStateInterface
function _allowed_formats_field_types() {
return ['text', 'text_long', 'text_with_summary'];
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function allowed_formats_field_config_presave(FieldConfigInterface $field_config): void {
// Ensure backwards compatibility for default configuration not converted yet
// to Drupal 10.1.x.
// @see https://www.drupal.org/node/3318572
// @see https://www.drupal.org/project/allowed_formats/issues/3324446
_allowed_formats_convert_formats2core($field_config);
}
/**
* Converts allowed formats to Drupal 10.1.x.
*
* @param \Drupal\field\FieldConfigInterface $field_config
* The field configuration entity.
*
* @return bool
* Whether the field configuration has been changed.
*
* @see https://www.drupal.org/node/3318572
*/
function _allowed_formats_convert_formats2core(FieldConfigInterface $field_config): bool {
// Can't use $field_config->getThirdPartySettings() because that doesn't
// reveal if allowed_formats module key exists. If the module key exists, even
// if it's empty, we want to remove it.
if (!array_key_exists('allowed_formats', $field_config->get('third_party_settings') ?? [])) {
return FALSE;
}
$allowed_formats = $field_config->getThirdPartySetting('allowed_formats', 'allowed_formats', []);
// On some installations it might be possible that earlier module versions
// stored the formats as they are submitted from a checkbox list:
// @code
// ['format1' => '0', 'format2' => 'format2', 'format3' => '0']
// @encode
$allowed_formats = array_values(array_filter($allowed_formats));
// At the time when the site builder updates this module, the allowed
// format field setting, provided by core, might already contain data, but
// that is irrelevant because the allowed formats provided by this module
// takes precedence in the form. So, it's safe to override potential field
// settings added via new Drupal >10.0.0 feature.
$field_config
->setSetting('allowed_formats', $allowed_formats)
->unsetThirdPartySetting('allowed_formats', 'allowed_formats');
return TRUE;
}
......@@ -5,7 +5,9 @@
* Post update functions for Allowed Formats module.
*/
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\FieldConfigInterface;
/**
* Updates existing configuration to store allowed_formats as sequence.
......@@ -32,3 +34,12 @@ function allowed_formats_post_update_store_allowed_formats_as_sequence() {
return t('Allowed formats in field configuration has been updated.');
}
/**
* Convert allowed formats to Drupal 10.1.x.
*/
function allowed_formats_post_update_formats2core(?array &$sandbox = NULL): void {
\Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'field_config', function (FieldConfigInterface $field_config): bool {
return _allowed_formats_convert_formats2core($field_config);
});
}
{
"name": "drupal/allowed_formats",
"description": "Limit which text formats are available for each field instance.",
"description": "Hides info about the selected text format. The 'allowed formats' functionality has been moved to core since Drupal 10.1.0.",
"type": "drupal-module",
"homepage": "https://www.drupal.org/project/allowed_formats",
"license": "GPL-2.0-or-later",
......@@ -31,7 +31,7 @@
"issues": "https://www.drupal.org/project/issues/allowed_formats",
"source": "http://cgit.drupalcode.org/allowed_formats"
},
"require": {
"drupal/core": "^9.2 || ^10"
"conflict": {
"drupal/core": "<10.1.0"
}
}
field.field.*.*.*.third_party.allowed_formats:
type: mapping
label: 'Allowed formats configuration'
mapping:
allowed_formats:
type: sequence
label: Allowed formats
sequence:
type: string
label: Allowed format
field.widget.third_party.allowed_formats:
type: mapping
label: Allowed formats
......
<?php
/**
* @file
* Fixtures to test config conversion to Drupal 10.1.
*
* @see \Drupal\Tests\allowed_formats\Functional\Formats2CoreUpdateTest::testFormats2Core()
*/
use Drupal\Core\Database\Database;
$db = Database::getConnection();
// Enable allowed_formats module.
$extensions = unserialize($db->select('config')
->fields('config', ['data'])
->condition('collection', '')
->condition('name', 'core.extension')
->execute()
->fetchField());
$extensions['module']['allowed_formats'] = 0;
$db->update('config')
->fields(['data' => serialize($extensions)])
->condition('collection', '')
->condition('name', 'core.extension')
->execute();
// Limit formats for node.article.body.
$field_config = unserialize($db->select('config')
->fields('config', ['data'])
->condition('collection', '')
->condition('name', 'field.field.node.article.body')
->execute()
->fetchField());
$field_config['third_party_settings']['allowed_formats']['allowed_formats'] = [
'full_html',
'restricted_html',
];
// Also add allowed formats in the standard Drupal 10.1.x way, to test whether
// they are properly overridden.
$field_config['settings']['allowed_formats'] = [
'basic_html',
];
$db->update('config')
->fields(['data' => serialize($field_config)])
->condition('collection', '')
->condition('name', 'field.field.node.article.body')
->execute();
name: 'Allowed formats base fields test'
type: module
description: 'Support module for allowed formats testing on base fields.'
package: Testing
dependencies:
drupal:taxonomy
drupal:allowed_formats
# Information added by Drupal.org packaging script on 2022-11-23
version: '2.0.0'
project: 'allowed_formats'
datestamp: 1669170415
<?php
/**
* @file
* Contains allowed_formats_base_fields_test.module.
*/
/**
* Implements hook_entity_base_field_info_alter().
*/
function allowed_formats_base_fields_test_entity_base_field_info_alter(&$fields, $entity_type) {
if ($entity_type->id() == 'taxonomy_term') {
// Configure allowed format settings.
$fields['description']->setSetting('allowed_formats', ['basic_html' => 'basic_html', 'restricted_html' => 'restricted_html']);
}
}
<?php
namespace Drupal\Tests\allowed_formats\Functional;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Tests the basic functionality of Allowed Formats.
*
* @group allowed_formats
*/
class AllowedFormatsTest extends BrowserTestBase {
// Provides shortcut method createVocabulary().
use TaxonomyTestTrait;
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'entity_test',
'allowed_formats',
'field_ui',
'taxonomy',
];
/**
* A user with relevant administrative privileges.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* A user with privileges to edit a text field.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'administer filters',
'administer entity_test fields',
]);
$this->webUser = $this->drupalCreateUser([
'administer entity_test content',
'administer taxonomy',
]);
}
/**
* Test widgets for fields with selected allowed formats.
*/
public function testAllowedFormats() {
// Create one text format.
$format1 = FilterFormat::create([
'format' => mb_strtolower($this->randomMachineName()),
'name' => $this->randomString(),
'roles' => [$this->webUser->getRoles()[0]],
]);
$format1->save();
// Create a second text format.
$format2 = FilterFormat::create([
'format' => mb_strtolower($this->randomMachineName()),
'name' => $this->randomString(),
'roles' => [$this->webUser->getRoles()[0]],
]);
$format2->save();
// Change the Allowed Formats settings of the test field created by
// entity_test_install().
$this->drupalLogin($this->adminUser);
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_test_text');
$this->submitForm([
'third_party_settings[allowed_formats][allowed_formats][' . $format1->id() . ']' => TRUE,
'third_party_settings[allowed_formats][allowed_formats][' . $format2->id() . ']' => TRUE,
], $this->t('Save settings'));
// Display the creation form.
$this->drupalLogin($this->webUser);
$this->drupalGet('entity_test/add');
$this->assertSession()->fieldExists("field_test_text[0][value]");
$this->assertSession()->fieldExists("field_test_text[0][format]");
// Change field to allow only one format.
$this->drupalLogin($this->adminUser);
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_test_text');
$this->submitForm([
'third_party_settings[allowed_formats][allowed_formats][' . $format2->id() . ']' => FALSE,
], $this->t('Save settings'));
// We shouldn't have the 'format' selector since only one format is allowed.
$this->drupalLogin($this->webUser);
$this->drupalGet('entity_test/add');
$this->assertSession()->fieldExists("field_test_text[0][value]");
$this->assertSession()->fieldNotExists("field_test_text[0][format]");
}
/**
* Test limiting allowed formats on base fields.
*/
public function testBaseFields() {
// Create a vocabulary.
$vocabulary = $this->createVocabulary();
// Create the text formats as configured for the taxonomy term description
// field.
$roles = [$this->webUser->getRoles()[0]];
$format1 = FilterFormat::create([
'format' => 'basic_html',
'name' => 'basic_html',
'roles' => $roles,
]);
$format1->save();
$format2 = FilterFormat::create([
'format' => 'restricted_html',
'name' => 'restricted_html',
'roles' => $roles,
]);
$format2->save();
$format3 = FilterFormat::create([
'format' => 'full_html',
'name' => 'full_html',
'roles' => $roles,
]);
$format3->save();
// Display the term creation form, we expect the widget to be displayed,
// and the formats 'basic_html', 'restricted_html' and 'full_html' to be
// available.
$this->drupalLogin($this->webUser);
$this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
$this->assertSession()->fieldExists("description[0][value]");
$this->assertSession()->fieldExists("description[0][format]");
$this->assertSession()->optionExists('edit-description-0-format--2', 'basic_html');
$this->assertSession()->optionExists('edit-description-0-format--2', 'restricted_html');
$this->assertSession()->optionExists('edit-description-0-format--2', 'full_html');
// Enable our test module, which disallows using the 'full_html' format
// using the allowed_formats functionality.
\Drupal::service('module_installer')->install(['allowed_formats_base_fields_test']);
// Display the term creation form again and check that 'full_html' is
// not available as expected.
$this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
$this->assertSession()->fieldExists("description[0][value]");
$this->assertSession()->fieldExists("description[0][format]");
$this->assertSession()->optionExists('edit-description-0-format--2', 'basic_html');
$this->assertSession()->optionExists('edit-description-0-format--2', 'restricted_html');
$this->assertSession()->optionNotExists('edit-description-0-format--2', 'full_html');
}
}
<?php
namespace Drupal\Tests\allowed_formats\Functional;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* @group allowed_formats
* @group legacy
*/
class Formats2CoreUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles(): void {
$this->databaseDumpFiles = [
DRUPAL_ROOT . '/core/modules/system/tests/fixtures/update/drupal-9.4.0.filled.standard.php.gz',
__DIR__ . '/../../fixtures/update/allowed_formats_post_update_formats2core.php',
];
}
/**
* @covers \allowed_formats_post_update_formats2core
*/
public function testFormats2Core(): void {
$config = $this->config('field.field.node.article.body');
// Check that, before update, allowed formats exist as 3rd-party settings.
$this->assertSame(['full_html', 'restricted_html'], $config->get('third_party_settings.allowed_formats.allowed_formats'));
// Check that, before update, allowed formats exist as field settings.
$this->assertSame(['basic_html'], $config->get('settings.allowed_formats'));
$this->runUpdates();
$config = $this->config('field.field.node.article.body');
// Check that, after update, allowed_formats 3rd-party settings are removed.
$this->assertArrayNotHasKey('allowed_formats', $config->get('third_party_settings') ?? []);
// Check that, after update, allowed formats were set from module settings.
$this->assertSame(['full_html', 'restricted_html'], $config->get('settings.allowed_formats'));
}
}
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