You are here

class Callback in Menu item content fields 8

Provides a trusted callbacks to alter some elements markup.

Hierarchy

Expanded class hierarchy of Callback

See also

menu_item_fields_preprocess_menu__field_content()

1 file declares its use of Callback
menu_item_fields.module in ./menu_item_fields.module
Contains hooks for the Menu Item Fields module.

File

src/Render/Callback.php, line 12

Namespace

Drupal\menu_item_fields\Render
View source
class Callback implements TrustedCallbackInterface {

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'preRenderMenuLinkContent',
    ];
  }

  /**
   * Fill the the link field with values from the menu item.
   *
   * #pre_render callback.
   */
  public static function preRenderMenuLinkContent($element) {

    // We skip processing if link field is not in display output.
    if (!isset($element['link'])) {
      return $element;
    }
    $contentLink =& $element['link'][0];
    $contentUrl =& $contentLink['#url'];

    // Set the title attribute (description field) from the menu item.
    $menuItemAttributes = $element['#menu_item']['url']
      ->getOption('attributes');
    if (isset($menuItemAttributes['title'])) {
      $contentLinkAttributes = $contentUrl
        ->getOption('attributes');
      $contentLinkAttributes['title'] = $menuItemAttributes['title'];
      $contentUrl
        ->setOption('attributes', $contentLinkAttributes);
    }
    $contentUrl
      ->setOption('set_active_class', $element['#menu_item']['url']
      ->getOption('set_active_class'));
    if (is_string($contentLink['#title'])) {
      $contentLink['#title'] = $element['#menu_item']['title'];
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Callback::preRenderMenuLinkContent public static function Fill the the link field with values from the menu item.
Callback::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.