Problem/Motivation

When adding a library item at the moment there is no way to know whether the item is published or not.

Proposed resolution

Add a formatter that adds the unpublished icon when needed.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

yongt9412 created an issue. See original summary.

johnchque’s picture

Status: Active » Needs review
StatusFileSize
new3.5 KB

This should be working. :)

berdir’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Lets add some test for this.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new5.32 KB
new1.6 KB

First tests. Let's give a try. :)

Status: Needs review » Needs work

The last submitted patch, 4: 3134860-item-summary-formatter-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new6.17 KB
new4.04 KB

Moving the tests. This time with Content moderation. :)

I've noticed that the tests of Library and Content Moderation are using the Legacy Widget. Opening a new issue for that: #3142680: Switch field widget for library tests to stable.

berdir’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests
  1. +++ b/modules/paragraphs_library/src/Plugin/Field/FieldFormatter/LibraryItemSummaryFormatter.php
    @@ -0,0 +1,79 @@
    +        if (!$items->getEntity()->isPublished()) {
    +          $published = [
    +            '#theme' => 'paragraphs_info_icon',
    +            '#message' => $this->t('Unpublished'),
    +            '#icon' => 'view',
    +          ];
    +          $elements[$delta]['info'] += $published;
    +        }
    +      }
    

    I think we can re-use the existing class here for most things. a diff between the two files shows it is identical except that extra icon that we're adding.

    So try to do an extends ParagraphsSummaryFormatter here and then call parent::build() and then you only need to add this extra visibility icon. IMHO you could even skip the foreach loop, as we know that this is for the library item base field which has cardinality 1 and so only has delta 0.

  2. +++ b/modules/paragraphs_library/src/Plugin/Field/FieldFormatter/LibraryItemSummaryFormatter.php
    @@ -0,0 +1,79 @@
    +  /**
    +   * {@inheritdoc}
    +   */
    +  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    +    $target_type = $field_definition->getSetting('target_type');
    +    $paragraph_type = \Drupal::entityTypeManager()->getDefinition($target_type);
    +    if ($paragraph_type) {
    +      return $paragraph_type->isSubclassOf(ParagraphInterface::class);
    +    }
    +    return FALSE;
    +  }
    +
    

    isSubclassOf is deprecated, looks like this was copied from an older version of the other summary class.

    I first thought we can just drop this with the changed parent, but actually, that would make this formatter show up on all paragraph fields, we don't want that. WE only want it on the paragraph field on library items. So instead of this, check getTargetEntityTypeId() of $field_definition and the field name to only allow this being used on this specific field.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new5.09 KB
new3.16 KB

Addressing suggestions!

berdir’s picture

Status: Needs review » Needs work

Needs a reroll as another issue added test logic in the same place.

mbovan’s picture

Status: Needs work » Needs review
StatusFileSize
new4.69 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 10: 3134860-item-summary-formatter-10.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mbovan’s picture

Status: Needs work » Needs review
StatusFileSize
new5.37 KB
new4.26 KB

Not sure if something has changed in Paragraphs default configuration since #8 to result in test #10 fails...

Fixed and improved tests.

berdir’s picture

Status: Needs review » Fixed

Hm, possibly the behavior change is the published-by-default change that the other issue did that this conflicted with? Test looks good to me, committed.

  • Berdir committed 33a15d7 on 8.x-1.x authored by mbovan
    Issue #3134860 by yongt9412, mbovan: Add a library item summary...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.