Drupal: Print field without markup

2019-07-25 04:14发布

问题:

Is there a way to print field content without getting all the markup? I'm new to Drupal, but I'm aware of the field.tpl.php, however, I'm just wondering if there's a quicker way to get the content in a node--custom.tpl.php. It would compare to Wordpress's <?php echo get_field('field_name'); ?>

回答1:

Well, apart from using field.tpl.php, I can think of 2 solutions:

first:

Use a php snippet to strip html tags in your template.php.

in your template.php

function mytheme_strip_html_tags($n_field) {

   return preg_replace("/<.*?>/", "", $n_field);

}

then call the function mytheme_strip_html_tags($field_name)

if you use several themes, however, you need to copy this snippet to each one of them.

EDIT: You can make a module and place that snippet inside. This way it works with every theme.

second:

Download the tokens module. Tokens are references to your fields. Tokens module have a output mode that strips html for you. [field_name-raw]

You need to follow instructions in how to add tokens, but is not that difficult.



回答2:

You have access to the $node variable inside a node.tpl.php, so:

<?php print $node->field_monkey_height; ?>

should work... note that many fields will hide their data inside arrays (for multiple value fields, etc) so you may need to do a bit of:

<?php drupal_set_message(print_r($node->field_monkey_height), 1); ?>

...to figure out the exact path to the data you need.



回答3:

You simply can use PHP's strip_tags() like so:

<?php print strip_tags($node->field_name[LANGUAGE_NONE][0]['value']); ?>


标签: php drupal