3

我需要将 HTML 标记添加到#titleDrupal 7#type链接表单元素的字段。输出应大致如下所示:

<a href="/saveprogress/nojs/123" id="saveprogress-link" class="ajax-processed">
  <span data-icon="&#61515;" aria-hidden="true" class="mymodule_symbol"></span>
  Save Progress
</a>

由于我在做一些 ajax 表单,我不能只使用#markupl()运行。这是一个没有跨度的示例:

function mymodule_save_progress_link($nid) {
  return array(
    '#type' => 'link',
    '#title' => t('Save Progress'),
    '#href' => 'saveprogress/nojs/' . $nid,
    '#id' => 'saveprogress-link',
    '#ajax' => array(
      'wrapper' => 'level-form',
      'method' => 'html',
    ),
  );
}

function mymodule_print_links($nid=NULL) {
  ctools_include('ajax');
  ctools_include('modal');
  ctools_modal_add_js();

  $build['saveprogress_link'] = mymodule_save_progress_link($nid);

  return '<div id="level-form">' . drupal_render($build) . '</div>';
}

当我添加<span>到该#title字段时,它被转义而不被解释为 HTML。如何将此跨度(或其他标记)插入到link类型表单元素的 tile 字段中。这个表单元素在 Drupal 站点上没有很好的记录。

4

2 回答 2

4

实际上有一种比自定义滚动主题更简单的方法 - 只需告诉drupal_render()'#title'其视为 html。

function mymodule_save_progress_link($nid) {
  return array(
    '#type' => 'link',
    '#title' => '<span>unescaped HTML here</span> '.t('Save Progress'),
    '#href' => 'saveprogress/nojs/' . $nid,
    '#id' => 'saveprogress-link',
    '#ajax' => array(
      'wrapper' => 'level-form',
      'method' => 'html',
    ),
    '#options' => array(
      'html' => true,
    )
  );
}

这对于将图像或其他元素添加到可点击区域非常方便。

于 2013-03-07T21:04:56.957 回答
0

我确信有更好的方法,但这是使用自定义主题的一种工作方法。您需要在 hook_theme() 中注册自定义主题功能,然后禁用并重新启用您的模块以更新主题注册表。在您的自定义主题函数中,您可以重写输出 HTML,但您需要添加一个不同的类“use-ajax”。

/**
 * Implements hook_theme().
 */
function mymodule_theme() {
  return array (
    'mymodule_link' => array(
      'render element' => 'element',
    ),
  );
}

/**
* Returns HTML for a mymodule link
*
* @param $variables
*   An associative array containing:
*   - element: A render element containing the properties of the link.
*
* @ingroup themeable
*/
function theme_mymodule_link($variables) {
  return l(
    '<span data-icon="&#61515;" '.
      'aria-hidden="true" class="mymodule-symbol"></span> '.
      $variables['element']['#title'],
    $variables['element']['#href'],
    array(
     'html' => TRUE, 
     'attributes' => array(
       'class' => array('use-ajax'), 
       'title' => $variables['element']['#title']
     )
    )
  );
}

最后,要求表单元素使用这个主题:

function mymodule_save_progress_link($nid) {
  return array(
    '#type' => 'link',
    '#title' => t('Save Progress'),
    '#href' => 'saveprogress/nojs/' . $nid,
    '#id' => 'saveprogress-link',
    '#ajax' => array(
      'wrapper' => 'level-form',
      'method' => 'html',
    ),
    '#theme' => 'mymodule_link',
  );
}
于 2013-02-28T19:48:00.273 回答