Skip to content
Snippets Groups Projects
Commit 2a60eb64 authored by Michael Lee's avatar Michael Lee
Browse files

Merge branch 'allowed_formats' into lee5151

parents 62f37ad7 72b680f5
No related branches found
No related tags found
No related merge requests found
......@@ -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": "ddbb6bfcc5492e80908375b84ecb45bb",
"content-hash": "26c2b8b285d17e928ff630e8f4787e0d",
"packages": [
{
"name": "alchemy/zippy",
......@@ -1843,26 +1843,26 @@
},
{
"name": "drupal/allowed_formats",
"version": "1.3.0",
"version": "1.5.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/allowed_formats.git",
"reference": "8.x-1.3"
"reference": "8.x-1.5"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/allowed_formats-8.x-1.3.zip",
"reference": "8.x-1.3",
"shasum": "4c3c036d7b41428d6e22b61f1219de0ab012feec"
"url": "https://ftp.drupal.org/files/projects/allowed_formats-8.x-1.5.zip",
"reference": "8.x-1.5",
"shasum": "dbf61bee7aec87beaa2cf307c1d0d9d5b896328c"
},
"require": {
"drupal/core": "^8.7.7 || ^9"
"drupal/core": "^8.8 || ^9"
},
"type": "drupal-module",
"extra": {
"drupal": {
"version": "8.x-1.3",
"datestamp": "1592909219",
"version": "8.x-1.5",
"datestamp": "1648060331",
"security-coverage": {
"status": "covered",
"message": "Covered by Drupal's security advisory policy"
......@@ -1875,18 +1875,31 @@
],
"authors": [
{
"name": "floretan",
"homepage": "https://www.drupal.org/user/66163"
"name": "Northern Commerce (formerly Digital Echidna)",
"homepage": "https://www.drupal.org/northern-commerce-formerly-digital-echidna",
"role": "Supporting organization"
},
{
"name": "Jordan Thompson (nord102)",
"homepage": "https://www.drupal.org/u/nord102",
"role": "Maintainer"
},
{
"name": "Wunder",
"homepage": "https://www.drupal.org/wunder",
"role": "Supporting organization"
},
{
"name": "nord102",
"homepage": "https://www.drupal.org/user/3471419"
"name": "Florian Loretan (floretan)",
"homepage": "https://www.drupal.org/u/floretan",
"role": "Maintainer"
}
],
"description": "Limit which text formats are available for each field instance.",
"homepage": "https://www.drupal.org/project/allowed_formats",
"support": {
"source": "https://git.drupalcode.org/project/allowed_formats"
"source": "http://cgit.drupalcode.org/allowed_formats",
"issues": "https://www.drupal.org/project/issues/allowed_formats"
}
},
{
......
......@@ -1890,27 +1890,27 @@
},
{
"name": "drupal/allowed_formats",
"version": "1.3.0",
"version_normalized": "1.3.0.0",
"version": "1.5.0",
"version_normalized": "1.5.0.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/allowed_formats.git",
"reference": "8.x-1.3"
"reference": "8.x-1.5"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/allowed_formats-8.x-1.3.zip",
"reference": "8.x-1.3",
"shasum": "4c3c036d7b41428d6e22b61f1219de0ab012feec"
"url": "https://ftp.drupal.org/files/projects/allowed_formats-8.x-1.5.zip",
"reference": "8.x-1.5",
"shasum": "dbf61bee7aec87beaa2cf307c1d0d9d5b896328c"
},
"require": {
"drupal/core": "^8.7.7 || ^9"
"drupal/core": "^8.8 || ^9"
},
"type": "drupal-module",
"extra": {
"drupal": {
"version": "8.x-1.3",
"datestamp": "1592909219",
"version": "8.x-1.5",
"datestamp": "1648060331",
"security-coverage": {
"status": "covered",
"message": "Covered by Drupal's security advisory policy"
......@@ -1924,14 +1924,31 @@
],
"authors": [
{
"name": "floretan",
"homepage": "https://www.drupal.org/user/66163"
"name": "Northern Commerce (formerly Digital Echidna)",
"homepage": "https://www.drupal.org/northern-commerce-formerly-digital-echidna",
"role": "Supporting organization"
},
{
"name": "Jordan Thompson (nord102)",
"homepage": "https://www.drupal.org/u/nord102",
"role": "Maintainer"
},
{
"name": "Wunder",
"homepage": "https://www.drupal.org/wunder",
"role": "Supporting organization"
},
{
"name": "Florian Loretan (floretan)",
"homepage": "https://www.drupal.org/u/floretan",
"role": "Maintainer"
}
],
"description": "Limit which text formats are available for each field instance.",
"homepage": "https://www.drupal.org/project/allowed_formats",
"support": {
"source": "https://git.drupalcode.org/project/allowed_formats"
"source": "http://cgit.drupalcode.org/allowed_formats",
"issues": "https://www.drupal.org/project/issues/allowed_formats"
},
"install-path": "../../web/modules/allowed_formats"
},
......
......@@ -5,7 +5,7 @@
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => 'e84dd64613669ee59f663ea0db62b53e321ee2c8',
'reference' => 'bc02f6fd4109ae90aaed2f0870deb0779978d71c',
'name' => 'osu-asc-webservices/d8-upstream',
'dev' => true,
),
......@@ -308,12 +308,12 @@
),
),
'drupal/allowed_formats' => array(
'pretty_version' => '1.3.0',
'version' => '1.3.0.0',
'pretty_version' => '1.5.0',
'version' => '1.5.0.0',
'type' => 'drupal-module',
'install_path' => __DIR__ . '/../../web/modules/allowed_formats',
'aliases' => array(),
'reference' => '8.x-1.3',
'reference' => '8.x-1.5',
'dev_requirement' => false,
),
'drupal/anchor_link' => array(
......@@ -2101,7 +2101,7 @@
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => 'e84dd64613669ee59f663ea0db62b53e321ee2c8',
'reference' => 'bc02f6fd4109ae90aaed2f0870deb0779978d71c',
'dev_requirement' => false,
),
'pantheon-systems/quicksilver-pushback' => array(
......
name: Allowed Formats
description: Limit which text formats are available for each field instance.
type: module
core_version_requirement: ^8.7.7 || ^9
core_version_requirement: ^8.8 || ^9
dependencies:
- drupal:field
- drupal:filter
# Information added by Drupal.org packaging script on 2020-06-23
version: '8.x-1.3'
# Information added by Drupal.org packaging script on 2022-03-23
version: '8.x-1.5'
project: 'allowed_formats'
datestamp: 1592909221
datestamp: 1648060332
......@@ -26,16 +26,33 @@ function allowed_formats_form_field_config_edit_form_alter(array &$form, FormSta
$options[$format->id()] = $format->label();
}
$form['third_party_settings']['allowed_formats'] = [
$settings = $field->getThirdPartySettings('allowed_formats');
$form['third_party_settings']['allowed_formats']['allowed_formats'] = [
'#type' => 'checkboxes',
'#title' => t('Allowed formats'),
'#options' => $options,
'#default_value' => $field->getThirdPartySettings('allowed_formats'),
'#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)));
}
/**
* Implements hook_field_widget_third_party_settings_form().
*/
......@@ -72,7 +89,7 @@ function allowed_formats_field_widget_form_alter(&$element, FormStateInterface $
// 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->getThirdPartySettings('allowed_formats');
$allowed_formats_setting = $field_configuration->getThirdPartySetting('allowed_formats', 'allowed_formats');
}
else {
// Base fields don't support third party settings so use an ordinary
......@@ -81,11 +98,8 @@ function allowed_formats_field_widget_form_alter(&$element, FormStateInterface $
$allowed_formats_setting = $field_definition->getSetting('allowed_formats');
}
if (isset($allowed_formats_setting) && is_array($allowed_formats_setting)) {
$allowed_formats = array_filter($allowed_formats_setting);
if (!empty($allowed_formats)) {
$element['#allowed_formats'] = $allowed_formats;
}
if (!empty($allowed_formats_setting) && is_array($allowed_formats_setting)) {
$element['#allowed_formats'] = $allowed_formats_setting;
}
/** @var \Drupal\Core\Field\WidgetInterface $widget */
......
<?php
/**
* @file
* Post update functions for Allowed Formats module.
*/
use Drupal\field\Entity\FieldConfig;
/**
* Updates existing configuration to store allowed_formats as sequence.
*/
function allowed_formats_post_update_store_allowed_formats_as_sequence() {
foreach (FieldConfig::loadMultiple() as $field_config) {
/** @var \Drupal\field\Entity\FieldConfig $field_config */
if (in_array($field_config->getType(), _allowed_formats_field_types(), TRUE)) {
$allowed_formats = $field_config->getThirdPartySettings('allowed_formats');
if (!empty($allowed_formats)) {
// Don't do anything if the configuration is already a sequence.
if (isset($allowed_formats['allowed_formats']) && is_array($allowed_formats['allowed_formats'])) {
continue;
}
// Unset existing configuration.
foreach ($allowed_formats as $key => $value) {
$field_config->unsetThirdPartySetting('allowed_formats', $key);
}
$field_config->setThirdPartySetting('allowed_formats', 'allowed_formats', array_values(array_filter($allowed_formats)));
$field_config->save();
}
}
}
return t('Allowed formats in field configuration has been updated.');
}
{
"name": "drupal/allowed_formats",
"description": "Limit which text formats are available for each field instance.",
"type": "drupal-module",
"homepage": "https://www.drupal.org/project/allowed_formats",
"license": "GPL-2.0-or-later",
"minimum-stability": "dev",
"authors": [
{
"name": "Northern Commerce (formerly Digital Echidna)",
"homepage": "https://www.drupal.org/northern-commerce-formerly-digital-echidna",
"role": "Supporting organization"
},
{
"name": "Jordan Thompson (nord102)",
"homepage": "https://www.drupal.org/u/nord102",
"role": "Maintainer"
},
{
"name": "Wunder",
"homepage": "https://www.drupal.org/wunder",
"role": "Supporting organization"
},
{
"name": "Florian Loretan (floretan)",
"homepage": "https://www.drupal.org/u/floretan",
"role": "Maintainer"
}
],
"support": {
"issues": "https://www.drupal.org/project/issues/allowed_formats",
"source": "http://cgit.drupalcode.org/allowed_formats"
},
"require": {
"drupal/core": "^8.8 || ^9"
}
}
field.field.*.*.*.third_party.allowed_formats:
type: sequence
label: Allowed formats
sequence:
type: string
label: Allowed format
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
......
......@@ -7,7 +7,7 @@ dependencies:
drupal:taxonomy
drupal:allowed_formats
# Information added by Drupal.org packaging script on 2020-06-23
version: '8.x-1.3'
# Information added by Drupal.org packaging script on 2022-03-23
version: '8.x-1.5'
project: 'allowed_formats'
datestamp: 1592909221
datestamp: 1648060332
......@@ -5,6 +5,7 @@
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.
......@@ -15,6 +16,7 @@ class AllowedFormatsTest extends BrowserTestBase {
// Provides shortcut method createVocabulary().
use TaxonomyTestTrait;
use StringTranslationTrait;
/**
* {@inheritdoc}
......@@ -53,8 +55,14 @@ class AllowedFormatsTest extends BrowserTestBase {
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser(['administer filters', 'administer entity_test fields']);
$this->webUser = $this->drupalCreateUser(['administer entity_test content', 'administer taxonomy']);
$this->adminUser = $this->drupalCreateUser([
'administer filters',
'administer entity_test fields',
]);
$this->webUser = $this->drupalCreateUser([
'administer entity_test content',
'administer taxonomy',
]);
}
/**
......@@ -81,28 +89,30 @@ public function testAllowedFormats() {
// Change the Allowed Formats settings of the test field created by
// entity_test_install().
$this->drupalLogin($this->adminUser);
$this->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.field_test_text', [
'third_party_settings[allowed_formats][' . $format1->id() . ']' => TRUE,
'third_party_settings[allowed_formats][' . $format2->id() . ']' => TRUE,
], t('Save settings'));
$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->assertFieldByName("field_test_text[0][value]", NULL, 'Widget is displayed');
$this->assertFieldByName("field_test_text[0][format]", NULL, 'Format selector is displayed');
$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->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.field_test_text', [
'third_party_settings[allowed_formats][' . $format2->id() . ']' => FALSE,
], t('Save settings'));
$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->assertFieldByName("field_test_text[0][value]", NULL, 'Widget is displayed');
$this->assertNoFieldByName("field_test_text[0][format]", NULL, 'Format selector is not displayed');
$this->assertSession()->fieldExists("field_test_text[0][value]");
$this->assertSession()->fieldNotExists("field_test_text[0][format]");
}
/**
......@@ -139,11 +149,11 @@ public function testBaseFields() {
// available.
$this->drupalLogin($this->webUser);
$this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
$this->assertFieldByName("description[0][value]", NULL, 'Widget is displayed');
$this->assertFieldByName("description[0][format]", NULL, 'Format selector is displayed');
$this->assertOption('edit-description-0-format--2', 'basic_html');
$this->assertOption('edit-description-0-format--2', 'restricted_html');
$this->assertOption('edit-description-0-format--2', 'full_html');
$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.
......@@ -152,11 +162,11 @@ public function testBaseFields() {
// 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->assertFieldByName("description[0][value]", NULL, 'Widget is displayed');
$this->assertFieldByName("description[0][format]", NULL, 'Format selector is displayed');
$this->assertOption('edit-description-0-format--2', 'basic_html');
$this->assertOption('edit-description-0-format--2', 'restricted_html');
$this->assertNoOption('edit-description-0-format--2', 'full_html');
$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');
}
}
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