Problem/Motivation

The following methods are still in filter.module with no OO replacement:

  • filter_formats()
  • filter_formats_reset()
  • filter_get_roles_by_format()
  • filter_get_formats_by_role()
  • filter_default_format()
  • filter_fallback_format()

They are all for loading filter formats under certain circumstances, and many revolve around old drupal_static calls.

Splitting filter_formats() into two methods is especially important, as the extra $account parameter drastically changes the behavior of the function (whether or not any access checks should be performed).

Proposed resolution

Deprecate all 6 of these functions, add \Drupal\filter\FilterFormatRepositoryInterface (\Drupal::service('filter.filter_format_repository')) to replace them

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

CommentFileSizeAuthor
#66 interdiff_64-66.txt1.58 KBridhimaabrol24
#66 2536594-66.patch94.14 KBridhimaabrol24
#64 interdiff_62-64.txt2.66 KBridhimaabrol24
#64 2536594-64.patch93.94 KBridhimaabrol24
#62 interdiff_61-62.txt1.02 KBridhimaabrol24
#62 2536594-62.patch92.72 KBridhimaabrol24
#61 2536594-61.patch92.72 KBridhimaabrol24
#57 2536594-57.patch92.44 KBclaudiu.cristea
#56 2536594-56.patch14.06 KBclaudiu.cristea
#56 2536594-56.interdiff.txt14.06 KBclaudiu.cristea
#52 2536594-52.patch92.45 KBandypost
#52 interdiff.txt13.11 KBandypost
#50 2536594-50.patch94.08 KBandypost
#50 interdiff.txt4.92 KBandypost
#47 2536594-47.interdiff.txt48.48 KBclaudiu.cristea
#47 2536594-47.patch95.1 KBclaudiu.cristea
#44 2536594-44.interdiff.txt527 bytesclaudiu.cristea
#44 2536594-44.patch94.97 KBclaudiu.cristea
#42 2536594-42.patch94.97 KBclaudiu.cristea
#42 2536594-42.interdiff.txt4.85 KBclaudiu.cristea
#40 2536594-40.interdiff.txt50.35 KBclaudiu.cristea
#40 2536594-40.patch91 KBclaudiu.cristea
#37 2536594-37.patch91.81 KBclaudiu.cristea
#37 2536594-37.interdiff.txt1.29 KBclaudiu.cristea
#35 2536594-34.patch91.8 KBclaudiu.cristea
#35 2536594-34.interdiff.txt4.92 KBclaudiu.cristea
#32 2536594-32.patch91.44 KBclaudiu.cristea
#32 2536594-32.interdiff.txt2.19 KBclaudiu.cristea
#30 2536594-30.interdiff.txt19.46 KBclaudiu.cristea
#30 2536594-30.patch91.45 KBclaudiu.cristea
#29 2536594-29.interdiff.txt746 bytesclaudiu.cristea
#29 2536594-29.patch91.31 KBclaudiu.cristea
#27 2536594-27.interdiff.txt59.48 KBclaudiu.cristea
#27 2536594-27.patch92.04 KBclaudiu.cristea
#25 2536594-25.interdiff.txt59.48 KBclaudiu.cristea
#25 2536594-25.patch59.48 KBclaudiu.cristea
#22 2536594-22.patch34.78 KBclaudiu.cristea
#22 2536594-22.interdiff.txt22.63 KBclaudiu.cristea
#11 interdiff.txt7.6 KBtim.plunkett
#11 filter-2536594-11.patch35.44 KBtim.plunkett
#10 interdiff.txt2.1 KBdawehner
#9 filter-2536594-9.patch35.28 KBtim.plunkett
#9 interdiff.txt6.91 KBtim.plunkett
#7 interdiff.txt16.46 KBtim.plunkett
#7 filter-2536594-7.patch34.86 KBtim.plunkett
#5 interdiff.txt525 bytestim.plunkett
#5 filter-2536594-5.patch35.66 KBtim.plunkett
#3 interdiff.txt3.64 KBtim.plunkett
#3 filter-2536594-3.patch35.66 KBtim.plunkett
#1 filter-2536594-1.patch32.02 KBtim.plunkett
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Status: Active » Needs review
FileSize
32.02 KB

Status: Needs review » Needs work

The last submitted patch, 1: filter-2536594-1.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
35.66 KB
3.64 KB

Ran php core/scripts/generate-proxy-class.php '\Drupal\filter\FilterFormatRepository' "core/modules/filter/src" to fix that

Status: Needs review » Needs work

The last submitted patch, 3: filter-2536594-3.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
35.66 KB
525 bytes

You've gotta be kidding me. Why the hell is that not valid!?

dawehner’s picture

It is great to do those things, but I kinda doubt this will be able to land in 8.0.x

  1. +++ b/core/modules/filter/src/FilterFormatRepositoryInterface.php
    @@ -0,0 +1,113 @@
    +  /**
    +   * Gets only those formats that are allowed for this user account.
    +   *
    

    Define what means allowed

  2. +++ b/core/modules/filter/src/FilterFormatRepositoryInterface.php
    @@ -0,0 +1,113 @@
    +   * Gets a list of text formats that are allowed for a given role.
    ...
    +   * Returns the default text format for a particular user.
    

    Any reason for gets vs. returns?

  3. +++ b/core/modules/filter/tests/src/Unit/FilterFormatRepositoryTest.php
    @@ -0,0 +1,309 @@
    +class FilterFormatRepositoryTest extends UnitTestCase {
    

    Did you considered mocking using prophecy? You should see the error messages created by prophecy, its from another universe

  4. +++ b/core/modules/filter/tests/src/Unit/FilterFormatRepositoryTest.php
    @@ -0,0 +1,309 @@
    +    $entity_type_definition = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
    

    I would argue that entity type definitions are value objects and so don't need to be mocked, as they don't consists of logic for themselves

tim.plunkett’s picture

1) Rewritten
2) Returns was c/p, changed all to Gets
3) Tried it out. Thanks for the pointers
4) Fair point!

dawehner’s picture

+++ b/core/modules/filter/tests/src/Unit/FilterFormatRepositoryTest.php
@@ -0,0 +1,274 @@
+    $current_language = $this->prophesize('\Drupal\Core\Language\LanguageInterface');

What about using ::class and get autocompletion?

tim.plunkett’s picture

I broke some of the coverage with that.

Also I have no idea what #8 is suggesting. @dawehner, do you want to implement what you have in mind?

dawehner’s picture

FileSize
2.1 KB

Sometimes along this lines ...

tim.plunkett’s picture

Oh cool, importing those really shows you how many things you depend on.
@dawehner++

Mile23’s picture

Status: Needs review » Needs work

Really like the Interface::class pattern. :-)

I don't suppose there's an easy way to do a test-only patch here.

+++ b/core/modules/filter/filter.module
@@ -85,174 +85,69 @@ function filter_theme() {
+ * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0. Use
+ *   either \Drupal\filter\FilterFormatRepositoryInterface::getAllFormats() or
+ *   \Drupal\filter\FilterFormatRepositoryInterface::getFormatsForAccount().

The patch in #11 still applies, but there's no FilterFormatRepositoryInterface anymore.

Mile23’s picture

Hey, you know what? git diff --name-only won't tell you files *added* by the patch. :-)

Mile23’s picture

My proxy-fu is no good, so I can't review that. Coding standards:

+++ b/core/modules/filter/src/FilterFormatRepositoryInterface.php
@@ -0,0 +1,113 @@
+   *
+   * @return \Drupal\filter\FilterFormatInterface
+   */
+  public function getDefaultFormat(AccountInterface $account = NULL);
+

Needs a return comment.

Mile23 queued 11: filter-2536594-11.patch for re-testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

claudiu.cristea’s picture

Added CR. Rerolled, refreshed the code, added proper deprecations.

claudiu.cristea’s picture

Adding #1577902: [META] Remove all usages of drupal_static() & drupal_static_reset() as related because this remove usages of drupal_static() and drupal_static_reset().

Status: Needs review » Needs work

The last submitted patch, 22: 2536594-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
59.48 KB
59.48 KB

Replaced all usages of deprecated functions.

Status: Needs review » Needs work

The last submitted patch, 25: 2536594-25.patch, failed testing. View results

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
92.04 KB
59.48 KB

The patch from #25 was wrong. Republishing.

Status: Needs review » Needs work

The last submitted patch, 27: 2536594-27.patch, failed testing. View results

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
91.31 KB
746 bytes

Ouch, an accidental edit.

claudiu.cristea’s picture

FileSize
91.45 KB
19.46 KB

::getDefaultFormat() was not properly used.

The last submitted patch, 29: 2536594-29.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

claudiu.cristea’s picture

FileSize
2.19 KB
91.44 KB

More fixes.

The last submitted patch, 30: 2536594-30.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 32: 2536594-32.patch, failed testing. View results

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
4.92 KB
91.8 KB

Fixing coding standards.

Status: Needs review » Needs work

The last submitted patch, 35: 2536594-34.patch, failed testing. View results

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
1.29 KB
91.81 KB

Last fix.

andypost’s picture

+++ b/core/modules/filter/filter.services.yml
@@ -9,3 +9,8 @@ services:
+  filter.filter_format_repository:

why not just filter.format_repository?

claudiu.cristea’s picture

I would say, better filter.format_repository. Because the service ID is always namespaced under the module’s name, which in this case is filter.*

EDIT: @andypost, initially I misread your comment. In fact we're on the same page.

claudiu.cristea’s picture

FileSize
91 KB
50.35 KB

Fixed #38. Improved the service.

Status: Needs review » Needs work

The last submitted patch, 40: 2536594-40.patch, failed testing. View results

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
4.85 KB
94.97 KB

Added deprecation test. Fixed unit test failure.

Status: Needs review » Needs work

The last submitted patch, 42: 2536594-42.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
94.97 KB
527 bytes

Coding standards.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

claudiu.cristea’s picture

Assigned: Unassigned » claudiu.cristea
Status: Needs review » Needs work
  1. +++ b/core/modules/comment/src/Plugin/migrate/source/d6/Comment.php
    @@ -85,7 +85,7 @@ public function fields() {
    -      'format' => $this->t('The {filter_formats}.format of the comment body.'),
    +      'format' => $this->t('The {filter_format}.format of the comment body.'),
    
    +++ b/core/modules/comment/src/Plugin/migrate/source/d7/Comment.php
    @@ -79,7 +79,7 @@ public function fields() {
    -      'format' => $this->t('The {filter_formats}.format of the comment body.'),
    +      'format' => $this->t('The {filter_format}.format of the comment body.'),
    

    Not related & wrong changes. Undo.

  2. +++ b/core/modules/filter/filter.module
    @@ -90,50 +89,36 @@ function filter_theme() {
    + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the
    ...
    +  @trigger_error('filter_formats() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::getAllFormats() to get all formats or with ::getFormatsForAccount() to get all formats that a user is able to access. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    ...
    + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the
    ...
    +  @trigger_error('filter_formats_reset() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::resetFormats() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    @@ -144,15 +129,15 @@ function filter_formats_reset() {
    + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the
    ...
    +  @trigger_error('filter_get_roles_by_format() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the \Drupal\filter\FilterFormatInterface::getRoles() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    @@ -164,16 +149,15 @@ function filter_get_roles_by_format(FilterFormatInterface $format) {
    + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the
    ...
    +  @trigger_error('filter_get_formats_by_role() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::getFormatsByRole() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    @@ -199,17 +183,14 @@ function filter_get_formats_by_role($rid) {
    + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the
    ...
    +  @trigger_error('filter_default_format() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::getDefaultFormat() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    @@ -238,17 +219,16 @@ function filter_default_format(AccountInterface $account = NULL) {
    + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the
    ...
    +  @trigger_error('filter_fallback_format() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::getFallbackFormatId() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    

    Increase version: 8.7.0 -> 8.8.0. Update also the CR.

  3. +++ b/core/modules/filter/src/Entity/FilterFormat.php
    @@ -409,6 +410,19 @@ public function removeFilter($instance_id) {
    +    // Handle the fallback format upfront (all roles have access to this format).
    

    Exceeds 80 chars.

  4. +++ b/core/modules/filter/src/FilterFormatListBuilder.php
    @@ -48,12 +55,15 @@ class FilterFormatListBuilder extends DraggableListBuilder {
    +   * @param \Drupal\filter\FilterFormatRepositoryInterface $format_repository
    +   *   The filter format repository service.
    ...
    +  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, FilterFormatRepositoryInterface $format_repository) {
    ...
    +    $this->formatRepository = $format_repository;
    

    For BC reasons $format_repository should be optional and should trigger deprecation error if not passed.

  5. +++ b/core/modules/filter/tests/src/Functional/FilterAdminTest.php
    @@ -187,8 +187,9 @@ public function testFilterAdmin() {
    +    $format_repository = $this->container->get('filter.format_repository');
    
    +++ b/core/modules/filter/tests/src/Functional/FilterDefaultFormatTest.php
    @@ -50,12 +50,13 @@ public function testDefaultTextFormats() {
    +    $format_repository = $this->container->get('filter.format_repository');
    
    @@ -65,14 +66,14 @@ public function testDefaultTextFormats() {
    +    $this->container->get('filter.format_repository')->resetFormats();
    
    +++ b/core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php
    @@ -118,9 +118,10 @@ protected function setUp() {
    +    $format_repository = $this->container->get('filter.format_repository');
    
    @@ -189,17 +190,18 @@ public function testFormatRoles() {
    +    $format_repository = $this->container->get('filter.format_repository');
    
    @@ -283,8 +285,10 @@ public function testFormatWidgetPermissions() {
    +    $format_repository = $this->container->get('filter.format_repository');
    
    @@ -296,7 +300,7 @@ public function testFormatWidgetPermissions() {
    +    foreach ($this->container->get('filter.format_repository')->getAllFormats() as $format) {
    
    @@ -328,7 +332,7 @@ public function testFormatWidgetPermissions() {
    +    $this->container->get('filter.format_repository')->resetFormats();
    
    +++ b/core/modules/filter/tests/src/Functional/FilterNoFormatTest.php
    @@ -31,7 +31,7 @@ public function testCheckMarkupNoFormat() {
    +    $this->assertEqual(check_markup($text), check_markup($text, $this->container->get('filter.format_repository')->getFallbackFormatId()), 'Text with no format is filtered the same as text in the fallback format.');
    
    +++ b/core/modules/filter/tests/src/Kernel/FilterAPITest.php
    @@ -486,7 +486,7 @@ public function testDependencyRemoval() {
    +    \Drupal::service('filter.format_repository')->resetFormats();
    
    +++ b/core/modules/filter/tests/src/Kernel/FilterCrudTest.php
    @@ -66,8 +66,8 @@ public function testTextFormatCrud() {
    +    $formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/filter/tests/src/Kernel/FilterSettingsTest.php
    @@ -47,7 +47,7 @@ public function testFilterDefaults() {
    +    $this->container->get('filter.format_repository')->resetFormats();
    
    +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php
    @@ -64,7 +64,7 @@ protected function setUp() {
    +          'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/node/tests/src/Functional/NodeRevisionsAllTest.php
    @@ -95,7 +95,7 @@ protected function createNodeRevision(NodeInterface $node) {
    +      'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/node/tests/src/Functional/NodeRevisionsTest.php
    @@ -105,7 +105,7 @@ protected function setUp() {
    +        'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/node/tests/src/Functional/NodeRevisionsUiTest.php
    @@ -99,7 +99,7 @@ public function testNodeRevisionDoubleEscapeFix() {
    +      'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/node/tests/src/Functional/Views/FilterNodeAccessTest.php
    @@ -57,7 +57,7 @@ protected function setUp($import_test_views = TRUE) {
    +              'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/node/tests/src/Functional/Views/FrontPageTest.php
    @@ -283,7 +283,7 @@ protected function doTestFrontPageViewCacheTags($do_assert_views_caches) {
    +            'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/node/tests/src/Functional/Views/PathPluginTest.php
    @@ -48,7 +48,7 @@ protected function setUp($import_test_views = TRUE) {
    +              'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/node/tests/src/Functional/Views/RowPluginTest.php
    @@ -49,7 +49,7 @@ protected function setUp($import_test_views = TRUE) {
    +              'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php
    @@ -56,7 +56,7 @@ protected function setUp() {
    +        'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/quickedit/src/Tests/QuickEditAutocompleteTermTest.php
    @@ -198,7 +198,7 @@ public function testAutocompleteQuickEdit() {
    +    $filter_formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php
    @@ -632,9 +632,10 @@ public function testFieldapiField() {
    +    $format_repository = $this->container->get('filter.format_repository');
    
    +++ b/core/modules/search/tests/src/Functional/SearchDateIntervalTest.php
    @@ -34,7 +34,7 @@ protected function setUp() {
    +    $default_format = $this->container->get('filter.format_repository')->getDefaultFormat()->id();
    
    +++ b/core/modules/search/tests/src/Functional/SearchLanguageTest.php
    @@ -45,7 +45,7 @@ protected function setUp() {
    +    $default_format = $this->container->get('filter.format_repository')->getDefaultFormat()->id();
    
    +++ b/core/modules/search/tests/src/Functional/SearchMultilingualEntityTest.php
    @@ -62,7 +62,7 @@ protected function setUp() {
    +    $default_format = $this->container->get('filter.format_repository')->getDefaultFormat()->id();
    
    +++ b/core/modules/system/tests/src/Functional/Menu/BreadcrumbTest.php
    @@ -127,7 +127,7 @@ public function testBreadCrumbs() {
    +    $filter_formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/taxonomy/src/Tests/TaxonomyTestTrait.php
    @@ -45,7 +45,7 @@ public function createVocabulary() {
    +    $filter_formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/taxonomy/tests/src/Functional/TaxonomyTestTrait.php
    @@ -40,7 +40,7 @@ public function createVocabulary() {
    +    $filter_formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyFieldFilterTest.php
    @@ -160,7 +160,7 @@ protected function assertPageCounts($path, $counts, $message) {
    +    $filter_formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTestBase.php
    @@ -137,7 +137,7 @@ protected function mockStandardInstall() {
    +    $filter_formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyTestBase.php
    @@ -166,7 +166,7 @@ protected function mockStandardInstall() {
    +    $filter_formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/text/tests/src/Functional/TextFieldTest.php
    @@ -177,7 +177,7 @@ public function _testTextfieldWidgetsFormatted($field_type, $widget_type) {
    +    foreach ($this->container->get('filter.format_repository')->getAllFormats() as $format) {
    
    @@ -216,7 +216,7 @@ public function _testTextfieldWidgetsFormatted($field_type, $widget_type) {
    +    $this->container->get('filter.format_repository')->resetFormats();
    
    +++ b/core/modules/text/tests/src/FunctionalJavascript/TextareaWithSummaryTest.php
    @@ -70,7 +70,7 @@ public function testTextSummaryBehavior() {
    +          'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/views/tests/src/Functional/DefaultViewsTest.php
    @@ -152,7 +152,7 @@ public function testDefaultViews() {
    +    $filter_formats = $this->container->get('filter.format_repository')->getAllFormats();
    
    +++ b/core/modules/views/tests/src/Functional/Plugin/DisplayFeedTest.php
    @@ -49,7 +49,7 @@ public function testFeedOutput() {
    +          'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    @@ -107,7 +107,7 @@ public function testFeedFieldOutput() {
    +          'format' => $this->container->get('filter.format_repository')->getDefaultFormat()->id(),
    
    +++ b/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php
    @@ -283,7 +283,7 @@ public function testTextInputRequired() {
    +    $display['display_options']['exposed_form']['options']['text_input_required_format'] = $this->container->get('filter.format_repository')->getDefaultFormat()->id();
    
    +++ b/core/modules/views/tests/src/Kernel/Handler/AreaTextTest.php
    @@ -55,7 +55,7 @@ public function testAreaText() {
    +    $view->display_handler->handlers['header']['area']->options['content']['format'] = $this->container->get('filter.format_repository')->getDefaultFormat()->id();
    
    +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
    @@ -907,7 +907,7 @@ public function testEntityReferenceComputedField() {
    +    $entity->field_test_text->format = $this->container->get('filter.format_repository')->getDefaultFormat()->id();
    

    Tests should use \Drupal::service(...) instead of $this->container->get(...).

Apart from the inline review, we should handle the case when a 3rd party code uses calls the cache reset directly drupal_cache_reset('filter_formats').

claudiu.cristea’s picture

Fixed #46.

andypost’s picture

+++ b/core/modules/filter/src/FilterFormatRepository.php
@@ -0,0 +1,174 @@
+      $cid = 'filter_formats:' . $this->languageManager->getCurrentLanguage()->getId();
+      if ($cache = $this->cacheBackend->get($cid)) {
+        $this->formatsAll = $cache->data;
...
+  public function resetFormats() {
+    unset($this->formatsAll);
+    $this->formatsByUser = [];

btw Entities are could be statically cached in storage handler and I'm sure better to relay on its caching instead of internal property
Also it needs __sleep() method to unset this properties on serialize

claudiu.cristea’s picture

btw Entities are could be statically cached in storage handler and I'm sure better to relay on its caching instead of internal property

Not sure I get this.

andypost’s picture

I mean kind of it, we already using static cache for user roles, so caching of formats also makes sense
I see no reason to abuse cache with already cached config entities (only sorting of formats could be moved to storage handler)

Status: Needs review » Needs work

The last submitted patch, 50: 2536594-50.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

andypost’s picture

Status: Needs work » Needs review
FileSize
13.11 KB
92.45 KB

A bit of clean-up

Status: Needs review » Needs work

The last submitted patch, 52: 2536594-52.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

claudiu.cristea’s picture

+++ b/core/modules/filter/src/FilterFormatRepository.php
@@ -0,0 +1,136 @@
+    $formats = $this->storage->loadByProperties(['status' => TRUE]);

I see your point, but this is a performance regression compared to #47, as ::loadByProperties() is doing a real query against the DB on each ::getAllFormats() call. So, it's not only sorting of formats that occurs every time.

andypost’s picture

Just checked \Drupal\Core\Entity\EntityStorageBase::loadByProperties() and worried as well(

Then I think we should always use loadMultiple() & filter/sort in repository method but makes sense to test & I sure there should not be big diff in performance

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
14.06 KB
14.06 KB

Then I think we should always use loadMultiple() & filter/sort ...

Yep, I fully agree. better than having another caching layer.

... but makes sense to test & I sure there should not be big diff in performance

Given that there are only few text formats, usually fewer than 10 I don't know if it make sense to benchmark.

  1. +++ b/core/includes/bootstrap.inc
    @@ -950,6 +950,12 @@ function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
    +      @trigger_error("Using drupal_static_reset() with 'filter_formats' as parameter is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use \\Drupal\\filter\\FilterFormatRepositoryInterface::resetFormats() instead. See https://www.drupal.org/node/3035368.", E_USER_DEPRECATED);
    
    +++ b/core/modules/filter/filter.module
    @@ -90,50 +89,36 @@ function filter_theme() {
    + * @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the
    + *   'filter.format_repository' service with ::getAllFormats() to get all
    + *   formats or with ::getFormatsForAccount() to get all formats that a user is
    + *   able to access.
    ...
    +  @trigger_error('filter_formats() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::getAllFormats() to get all formats or with ::getFormatsForAccount() to get all formats that a user is able to access. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    ...
    + * @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the
    + *   'filter.format_repository' service with ::resetFormats() method.
    ...
    +  @trigger_error('filter_formats_reset() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::resetFormats() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    @@ -144,15 +129,15 @@ function filter_formats_reset() {
    + * @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the
    + *   \Drupal\filter\FilterFormatInterface::getRoles() method.
    ...
    +  @trigger_error('filter_get_roles_by_format() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the \Drupal\filter\FilterFormatInterface::getRoles() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    @@ -164,16 +149,15 @@ function filter_get_roles_by_format(FilterFormatInterface $format) {
    + * @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the
    + *   'filter.format_repository' service with ::getFormatsByRole() method.
    ...
    +  @trigger_error('filter_get_formats_by_role() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::getFormatsByRole() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    @@ -199,17 +183,14 @@ function filter_get_formats_by_role($rid) {
    + * @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the
    + *   'filter.format_repository' service with ::getDefaultFormat() method.
    ...
    +  @trigger_error('filter_default_format() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::getDefaultFormat() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    @@ -238,17 +219,16 @@ function filter_default_format(AccountInterface $account = NULL) {
    + * @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the
    + *   'filter.format_repository' service with ::getFallbackFormatId()
    + *   method.
    ...
    +  @trigger_error('filter_fallback_format() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use the "filter.format_repository" service with ::getFallbackFormatId() method. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    
    +++ b/core/modules/filter/src/FilterFormatListBuilder.php
    @@ -48,12 +55,19 @@ class FilterFormatListBuilder extends DraggableListBuilder {
    +      @trigger_error('The $format_repository parameter will become mandatory before Drupal 9.0.0. See https://www.drupal.org/node/3035368.', E_USER_DEPRECATED);
    

    Use the standard deprecation messages.

  2. +++ b/core/modules/filter/src/FilterFormatRepository.php
    @@ -0,0 +1,136 @@
    +    $this->storage = $entity_type_manager->getStorage('filter_format');
    

    We should not do this in constructor. I hit an edge case, see https://www.drupal.org/project/drupal/issues/2863986#comment-12899598. Let's revert this

I also noticed that some tests are doing a useless cache clearing.

The failing test still needs some investigation.

claudiu.cristea’s picture

FileSize
92.44 KB

Sorry, the patch from #56 was wrong. Here's the correct version. The interdiff is correct.

Status: Needs review » Needs work

The last submitted patch, 57: 2536594-57.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ridhimaabrol24’s picture

Status: Needs work » Needs review
FileSize
92.72 KB

Reroll for 9.1.x and trying to fix test cases.

ridhimaabrol24’s picture

Fixing PHP lint error in #61

Status: Needs review » Needs work

The last submitted patch, 62: 2536594-62.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ridhimaabrol24’s picture

Status: Needs work » Needs review
FileSize
93.94 KB
2.66 KB

Fixing test cases.

Status: Needs review » Needs work

The last submitted patch, 64: 2536594-64.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ridhimaabrol24’s picture

Status: Needs work » Needs review
FileSize
94.14 KB
1.58 KB

Fixing failed test cases. Also regarding #54, loadbyproperties() has been used in several repository methods in core.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/filter/src/FilterFormatRepository.php
    @@ -0,0 +1,141 @@
    +   * {@inheritdoc}
    ...
    +  public function getAllFormats() {
    +    $formats = $this->storage->loadByProperties(['status' => TRUE]);
    +    uasort($formats, [$this->storage->getEntityType()->getClass(), 'sort']);
    +    return $formats;
    

    We seem to be losing both static and persistent caching here.

    I kinda get the persistent cache, but note that loadByProperties() is *not* cached and is pretty slow. We could add a lookup_keys for status to FilterFormat as well, that would help a bit, but it's still a database query. And that requires an upgrade path too. I don't think removing the caching here is a good idea.

    I see that this was discussed before, but even a loadMultiple() is still an extra query. Every time. See \Drupal\Core\Config\Entity\ConfigEntityStorage::doLoadMultiple(). We cache the list of all config entities of a given type.

  2. +++ b/core/modules/filter/src/FilterFormatRepository.php
    @@ -0,0 +1,141 @@
    +   */
    +  public function resetFormats() {
    +    $this->entityTypeManager->getStorage('filter_format')->resetCache();
    +    $this->formatsByUser = [];
    

    this on the other hand seems pointless. All actual changes to the filter formats already save them and imply invalidating the entity cache. And any change that does not change the format but e.g. changes user/role permissions, this is pointless.

  3. +++ b/core/modules/filter/src/Plugin/DataType/FilterFormat.php
    @@ -46,7 +46,7 @@ public function getSettableOptions(AccountInterface $account = NULL) {
         // @todo: Avoid calling functions but move to injected dependencies.
         return array_map(function ($format) {
           return $format->label();
    -    }, filter_formats($account));
    +    }, \Drupal::service('filter.format_repository')->getFormatsForAccount($account));
    

    the array map thing happens quite often, we could add a method for that.

  4. +++ b/core/modules/filter/src/ProxyClass/FilterFormatRepository.php
    @@ -0,0 +1,120 @@
    +<?php
    +// @codingStandardsIgnoreFile
    +
    +/**
    + * This file was generated via php core/scripts/generate-proxy-class.php 'Drupal\filter\FilterFormatRepository' "core/modules/filter/src".
    + */
    +
    +namespace Drupal\filter\ProxyClass {
    +
    

    interesting, where are we injecting it in the critical path but might not actually use it? The proxy actually adds overhead and it's only worth doing if we are saving calls. What you want to do instead IMHO is make sure that you don't do any extra work in the constructor, specifically instantiating the entity storage.

  5. +++ b/core/modules/views/tests/src/Functional/Plugin/DisplayFeedTest.php
    +++ b/core/modules/views/tests/src/Functional/Plugin/DisplayFeedTest.php
    @@ -58,7 +58,7 @@ public function testFeedOutput() {
    
    @@ -58,7 +58,7 @@ public function testFeedOutput() {
           'body' => [
             0 => [
               'value' => 'A paragraph',
    -          'format' => filter_default_format(),
    +          'format' => \Drupal::service('filter.format_repository')->getDefaultFormat()->id(),
             ],
    

    this seems like an old method that no longer exists like this, used quite a lot.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.