Use a renderable array for link buttons

Created on 12 April 2023, over 1 year ago
Updated 6 June 2023, over 1 year ago

Problem/Motivation

In order to make link labels easier to override, we should use a renderable array.

In https://git.drupalcode.org/project/intelligencebank/-/blob/4.0.x/modules...

I am seeing this


  $link_url = Url::fromRoute('id_dam_media.asset_browser_form');
  $link_url->setOptions([
    'attributes' => [
      'class' => ['use-ajax', 'button', 'button--primary'],
      'data-dialog-type' => 'modal',
      'data-dialog-options' => Json::encode([
        'dialogClass' => 'media-library-widget-modal',
        'width' => '75%',
        'height' => 'window.innerHeight',
        'minHeight' => 500,
      ]),
    ],
    'query' => $formState->get('media_library_state')->all(),
  ]);

  $form['ib_dam_container'] = [
    "#type" => "container",
    'asset_browser' => [
      '#type' => 'markup',
      '#markup' => Link::fromTextAndUrl(t('Open IntelligenceBank Browser'), $link_url)->toString(),
      '#attached' => ['library' => [
        'core/drupal.dialog.ajax',
        'ib_dam/ckeditor',
      ]]
    ],
  ];

But it could be this ($link->toRenderable();instead of using the '#markup' element) , and then it is easier to override the button text

    $url = Url::fromRoute('id_dam_media.asset_browser_form');
    $url->setOptions([
      'attributes' => [
        'class' => ['use-ajax', 'button', 'button--primary'],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'dialogClass' => 'media-library-widget-modal',
          'width' => '75%',
          'height' => 'window.innerHeight',
          'minHeight' => 500,
        ]),
      ],
      'query' => $form_state->get('media_library_state')->all(),
    ]);

    $link = Link::fromTextAndUrl(t('Open IntelligenceBank Browser'), $url);
    $renderable_link = $link->toRenderable();
    $renderable_link['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $form['ib_dam_container']['asset_browser'] = $renderable_link;

Please let me know if you think this is a possibility, and I can make a Merge Request.

✨ Feature request
Status

Fixed

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

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

Comments & Activities

Production build 0.71.5 2024