diff --git a/composer.json b/composer.json index 8de26d8ad57143a7b098312f4904d154fb2fc8a6..70812fb1e35efb27906fd91b57d4e7430c02edfa 100644 --- a/composer.json +++ b/composer.json @@ -91,7 +91,7 @@ "drupal/addtocalendar": "3.1", "drupal/admin_toolbar": "2.0", "drupal/administerusersbyrole": "2.0-alpha6", - "drupal/allowed_formats": "1.1", + "drupal/allowed_formats": "1.2", "drupal/anchor_link": "^1.6", "drupal/better_exposed_filters": "3.0-alpha6", "drupal/bigmenu": "^1.0@alpha", @@ -316,4 +316,4 @@ "php": "7.0.8" } } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index e304ef53bba33d66a4e3b4eb9dcf9b8e5df5f190..09d41b9e079d5645f429871527753bc28d504a18 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "9cebdfa2dca9a89bb18005ed1f9db499", + "content-hash": "15067dbc168ac86670343e142eaae7f9", "packages": [ { "name": "alchemy/zippy", @@ -2246,17 +2246,17 @@ }, { "name": "drupal/allowed_formats", - "version": "1.1.0", + "version": "1.2.0", "source": { "type": "git", "url": "https://git.drupalcode.org/project/allowed_formats.git", - "reference": "8.x-1.1" + "reference": "8.x-1.2" }, "dist": { "type": "zip", - "url": "https://ftp.drupal.org/files/projects/allowed_formats-8.x-1.1.zip", - "reference": "8.x-1.1", - "shasum": "b7bfc78d7e64f1df37ca6cad118a1ff8f7228b64" + "url": "https://ftp.drupal.org/files/projects/allowed_formats-8.x-1.2.zip", + "reference": "8.x-1.2", + "shasum": "50f8f80cf3326382c968521d34d3a113b37e32b3" }, "require": { "drupal/core": "*" @@ -2267,8 +2267,8 @@ "dev-1.x": "1.x-dev" }, "drupal": { - "version": "8.x-1.1", - "datestamp": "1492709942", + "version": "8.x-1.2", + "datestamp": "1573747386", "security-coverage": { "status": "covered", "message": "Covered by Drupal's security advisory policy" @@ -2288,7 +2288,7 @@ "description": "Limit which text formats are available for each field instance.", "homepage": "https://www.drupal.org/project/allowed_formats", "support": { - "source": "http://cgit.drupalcode.org/allowed_formats" + "source": "https://git.drupalcode.org/project/allowed_formats" } }, { diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 899a4506a1d473357d69d2564c08238e90f63dce..a4112b9ef2d5c3654790ec25444d904c5357bfa8 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -2315,18 +2315,18 @@ }, { "name": "drupal/allowed_formats", - "version": "1.1.0", - "version_normalized": "1.1.0.0", + "version": "1.2.0", + "version_normalized": "1.2.0.0", "source": { "type": "git", "url": "https://git.drupalcode.org/project/allowed_formats.git", - "reference": "8.x-1.1" + "reference": "8.x-1.2" }, "dist": { "type": "zip", - "url": "https://ftp.drupal.org/files/projects/allowed_formats-8.x-1.1.zip", - "reference": "8.x-1.1", - "shasum": "b7bfc78d7e64f1df37ca6cad118a1ff8f7228b64" + "url": "https://ftp.drupal.org/files/projects/allowed_formats-8.x-1.2.zip", + "reference": "8.x-1.2", + "shasum": "50f8f80cf3326382c968521d34d3a113b37e32b3" }, "require": { "drupal/core": "*" @@ -2337,8 +2337,8 @@ "dev-1.x": "1.x-dev" }, "drupal": { - "version": "8.x-1.1", - "datestamp": "1492709942", + "version": "8.x-1.2", + "datestamp": "1573747386", "security-coverage": { "status": "covered", "message": "Covered by Drupal's security advisory policy" @@ -2359,7 +2359,7 @@ "description": "Limit which text formats are available for each field instance.", "homepage": "https://www.drupal.org/project/allowed_formats", "support": { - "source": "http://cgit.drupalcode.org/allowed_formats" + "source": "https://git.drupalcode.org/project/allowed_formats" } }, { diff --git a/web/modules/allowed_formats/README.txt b/web/modules/allowed_formats/README.txt new file mode 100644 index 0000000000000000000000000000000000000000..815c61bdc60a26c8850f1157cb2539d6a0f16437 --- /dev/null +++ b/web/modules/allowed_formats/README.txt @@ -0,0 +1,60 @@ +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 +----------- + + * Florian Loretan (floretan) - https://www.drupal.org/u/floretan + +Supporting organization: + + * Wunder - https://www.drupal.org/wunder-group diff --git a/web/modules/allowed_formats/allowed_formats.info.yml b/web/modules/allowed_formats/allowed_formats.info.yml index 9433ad60c6f78cf66d5ea27591506f8fae5cd5fa..fe1aaf95abdd0867682dc45b080a6b6b07a72975 100644 --- a/web/modules/allowed_formats/allowed_formats.info.yml +++ b/web/modules/allowed_formats/allowed_formats.info.yml @@ -1,13 +1,12 @@ name: Allowed Formats description: Limit which text formats are available for each field instance. type: module -# core: 8.x +core: 8.x dependencies: - - field - - filter + - drupal:field + - drupal:filter -# Information added by Drupal.org packaging script on 2017-04-20 -version: '8.x-1.1' -core: '8.x' +# Information added by Drupal.org packaging script on 2019-11-14 +version: '8.x-1.2' project: 'allowed_formats' -datestamp: 1492709944 +datestamp: 1573747389 diff --git a/web/modules/allowed_formats/allowed_formats.module b/web/modules/allowed_formats/allowed_formats.module index 0f75ac63b40e43f4c5604f951a6ec2e7546758ef..bc2a3a42f850a9ad436f75eaf29b532e5d50b9fd 100644 --- a/web/modules/allowed_formats/allowed_formats.module +++ b/web/modules/allowed_formats/allowed_formats.module @@ -1,7 +1,14 @@ <?php +/** + * @file + * This is the allowed_formats module. + * + * It enables limiting which text formats are available for each fields. + */ + use Drupal\Core\Form\FormStateInterface; -use Drupal\field\Entity\FieldConfig; +use Drupal\Core\Field\FieldConfigBase; use Drupal\Core\Field\WidgetInterface; use Drupal\Core\Field\FieldDefinitionInterface; @@ -24,7 +31,7 @@ function allowed_formats_form_field_config_edit_form_alter(array &$form, FormSta '#title' => t('Allowed formats'), '#options' => $options, '#default_value' => $field->getThirdPartySettings('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.') + '#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.'), ]; } } @@ -34,7 +41,7 @@ function allowed_formats_form_field_config_edit_form_alter(array &$form, FormSta */ function allowed_formats_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) { if (in_array($field_definition->getType(), _allowed_formats_field_types())) { - $element = array(); + $element = []; $element['hide_help'] = [ '#type' => 'checkbox', '#title' => t('Hide the help link <em>About text formats</em>.'), @@ -57,26 +64,31 @@ function allowed_formats_field_widget_form_alter(&$element, FormStateInterface $ /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ $field_definition = $context['items']->getFieldDefinition(); - // Base fields are not configurable. - if (!$field_definition instanceof FieldConfig) { - return; - } - // We can't use the protected isDefaultValueWidget() method. $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) { - $field_configuration = $field_definition->getConfig($field_definition->getTargetBundle()); - $allowed_formats_setting = $field_configuration->getThirdPartySettings('allowed_formats'); + 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->getThirdPartySettings('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 (is_array($allowed_formats_setting)) { + 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; } } - /** @var WidgetInterface $widget */ + /** @var \Drupal\Core\Field\WidgetInterface $widget */ $widget = $context['widget']; $element['#allowed_format_hide_settings'] = $widget->getThirdPartySettings('allowed_formats'); @@ -87,7 +99,7 @@ function allowed_formats_field_widget_form_alter(&$element, FormStateInterface $ } /** - * #after_build callback. + * The #after_build callback for text widgets. */ function _allowed_formats_remove_textarea_help($form_element, FormStateInterface $form_state) { if (isset($form_element['format'])) { diff --git a/web/modules/allowed_formats/src/Tests/AllowedFormatsTest.php b/web/modules/allowed_formats/src/Tests/AllowedFormatsTest.php index 3ef177266ad1855d73656e1aacbd11c29a78b8a5..3d8eaceb04de6ac23a4a0da5779c6172c5e4031f 100644 --- a/web/modules/allowed_formats/src/Tests/AllowedFormatsTest.php +++ b/web/modules/allowed_formats/src/Tests/AllowedFormatsTest.php @@ -5,6 +5,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\filter\Entity\FilterFormat; use Drupal\simpletest\WebTestBase; +use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait; /** * Tests the basic functionality of Allowed Formats. @@ -13,18 +14,26 @@ */ class AllowedFormatsTest extends WebTestBase { + // Provides shortcut method createVocabulary(). + use TaxonomyTestTrait; + /** * Modules to enable. * * @var array */ - public static $modules = ['entity_test', 'allowed_formats', 'field_ui']; + public static $modules = [ + 'entity_test', + 'allowed_formats', + 'field_ui', + 'taxonomy', + ]; /** - * A user with relevant administrative privileges. - * - * @var \Drupal\user\UserInterface - */ + * A user with relevant administrative privileges. + * + * @var \Drupal\user\UserInterface + */ protected $adminUser; /** @@ -40,14 +49,14 @@ class AllowedFormatsTest extends WebTestBase { protected function setUp() { parent::setUp(); - $this->adminUser = $this->drupalCreateUser(array('administer filters', 'administer entity_test fields')); - $this->webUser = $this->drupalCreateUser(array('administer entity_test content')); + $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. */ - function testAllowedFormats() { + public function testAllowedFormats() { // Create one text format. $format1 = FilterFormat::create([ @@ -91,4 +100,59 @@ function testAllowedFormats() { $this->assertFieldByName("field_test_text[0][value]", NULL, 'Widget is displayed'); $this->assertNoFieldByName("field_test_text[0][format]", NULL, 'Format selector is not displayed'); } + + /** + * 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->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'); + + // 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->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'); + } + } diff --git a/web/modules/allowed_formats/tests/modules/allowed_formats_base_fields_test/allowed_formats_base_fields_test.info.yml b/web/modules/allowed_formats/tests/modules/allowed_formats_base_fields_test/allowed_formats_base_fields_test.info.yml new file mode 100644 index 0000000000000000000000000000000000000000..c24599bce273fa69a85602cb71449b91e46041ca --- /dev/null +++ b/web/modules/allowed_formats/tests/modules/allowed_formats_base_fields_test/allowed_formats_base_fields_test.info.yml @@ -0,0 +1,14 @@ +name: 'Allowed formats base fields test' +type: module +description: 'Support module for allowed formats testing on base fields.' +package: Testing +# version: VERSION +core: 8.x +dependencies: + drupal:taxonomy + drupal:allowed_formats + +# Information added by Drupal.org packaging script on 2019-11-14 +version: '8.x-1.2' +project: 'allowed_formats' +datestamp: 1573747389 diff --git a/web/modules/allowed_formats/tests/modules/allowed_formats_base_fields_test/allowed_formats_base_fields_test.module b/web/modules/allowed_formats/tests/modules/allowed_formats_base_fields_test/allowed_formats_base_fields_test.module new file mode 100644 index 0000000000000000000000000000000000000000..f65f2f2bb070107396b5bb5328aa25dcaca06e85 --- /dev/null +++ b/web/modules/allowed_formats/tests/modules/allowed_formats_base_fields_test/allowed_formats_base_fields_test.module @@ -0,0 +1,16 @@ +<?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']); + } +}