validate type formbase form drupal drupal-7 drupal-forms

formbase - Drupal 7: agregar HTML dentro de la entrada de#link form type?



formbase drupal 8 (2)

Estoy seguro de que hay una mejor manera, pero aquí hay un método de trabajo mediante el uso de un tema personalizado. Debe registrar la función de tema personalizado en hook_theme (), luego deshabilitar y volver a habilitar su módulo para actualizar el registro de temas. En su función de tema personalizado, puede volver a escribir el HTML de salida, pero deberá agregar una clase diferente, ''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''] ) ) ); }

Finalmente, requiere que el elemento de formulario use este tema:

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'', ); }

Necesito agregar el marcado HTML al campo #title de un elemento de formulario de enlace Drupal 7 #type . La salida debería verse más o menos así:

<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>

Como estoy haciendo algunas formas de ajax, no puedo usar las #markup y l() . Aquí hay un ejemplo sin el lapso:

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>''; }

Cuando agrego el campo <span> al campo #title , se escapó y no se interpretó como HTML. ¿Cómo puedo insertar este tramo (u otro marcado) en el campo de mosaico de un elemento de formulario de tipo de link ? Este elemento de formulario no está bien documentado en el sitio de Drupal.


De hecho, hay una manera mucho más sencilla que la drupal_render() personalizada de un tema: simplemente dile a drupal_render() que trate ''#title'' como 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, ) ); }

Esto podría ser muy útil para agregar imágenes u otros elementos a un área que pueda hacer clic.