modelAttributes is not documented on drupalElementStyles

Created on 5 December 2023, 9 months ago

Problem/Motivation

On Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media.php::113, an interesting usage of the key modelAttributes can be seen:

          $element_style_configuration[] = [
            'isDefault' => TRUE,
            'name' => $default_view_mode,
            'title' => $all_view_modes[$view_mode],
            'attributeName' => 'data-view-mode',
            'attributeValue' => $view_mode,
            'modelElements' => ['drupalMedia'],
            'modelAttributes' => [
              'drupalMediaType' => array_keys($media_bundles),
            ],
          ];

This option can be used via yml to further narrow down the list of supported attributes on the model element in use. A common use case: limit the bundles, i.e. make a drupalElemenStyle apply only to a few bundles:

custom_cke5_plugin:
    config:
      drupalElementStyles:
        copyright:
          - name: 'false'
            title: 'Hide copyright information'
            modelElements: [ 'drupalMedia' ]
            modelAttributes:
              drupalMediaType: ['image']

This is supported and works. Unfortunately, this is not documented on the API on DrupalElementStyleEditing and DrupalElementStyleDefinition.

Proposed resolution

Document the API for developers.

📌 Task
Status

Active

Version

10.2

Component
CKEditor 5 

Last updated 2 days ago

Created by

🇪🇸Spain idiaz.roncero Madrid

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @idiaz.roncero
  • Merge request !5687Document usage of modelAttributes → (Open) created by idiaz.roncero
  • 🇪🇸Spain idiaz.roncero Madrid

    Opened MR.

    The type of modelAttributes should be Iterable, as stated on the types of toMap:

    /**
     * Transforms object or iterable to map. Iterable needs to be in the format acceptable by the `Map` constructor.
     *
     * ```ts
     * map = toMap( { 'foo': 1, 'bar': 2 } );
     * map = toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
     * map = toMap( anotherMap );
     * ```
     *
     * @param data Object or iterable to transform.
     * @returns Map created from data.
     */
    export default function toMap<T>(data: {
        readonly [key: string]: T;
    } | Iterable<readonly [string, T]> | null | undefined): Map<string, T>;
    

    ...but as I can see, JSDoc does not support Iterable as a type, so I put object instead (even if any iterable will suffice)

    @prop {object} modelAttributes
         *   An object containing the supported attributes on the model element or
         *   elements.
    
Production build 0.71.5 2024