日期:2015-02-24 阅读:5923
dmandwp系统 - wordpress系统和DM系统区块建站>>
这几个api以前遇到过,但没有明白什么意思,最近通过项目才明白了。是很实用的。可以说是drupal人工桥的利器,drupal人工桥的本质就是调字段。
虽然drupal提供了后台模块,比如ds,panels等来布局(这更适合程序员或非技术的建站人员),但对于前端人员来说,还是调调字段来的方便。因为这样可以让前端事先写好html,而不受drupal的影响。
http://www.drupalden.co.uk/printing-entity-field-values-field_get_items-field_view_value-functions
http://stackoverflow.com/questions/11694770/print-drupal-field-view-field-value-only
This code demonstrates how to print out field values from an associated entity within a node. The two functions used are:
field_get_items - to get the entities field values
field_view_value - to render out the values
For the purpose of demonstration lets say you have a 'Shop' and 'Chain' content type. Within the 'Chain' content type you have general values, eg. chain name & logo. Within the 'Shop' content type you have details specific to one individual shop, eg. telephone number & address.
When creating a 'Shop' content type you can choose what 'Chain' to link it to with an entity reference field. When rendering out your 'Shop' node you then want to include some of the fields that were set within your 'Chain' node.
Here's examples of the code you would place in your 'Shop' content template (node--shop.tpl.php), in order to pull in those entity field value arrays.
Loading the entire entity & outputting formatted fields as well as raw field values
The below code demonstrates loading all the fields from an associated entity and then rending the fields and values using a combination of field_view_field output (which adheres to your display settings) & specific values.
// load fields from associated entity 'Chain' content type
$node = node_load($node->field_chain['und'][0]['target_id']);
// chain logo
$chainLogo = field_view_field('node', $node, 'field_chain_logo', ''); //---注意,这种方式,取决于后台的内容类型显示设置
// chain website
$chainSiteUrl = field_view_field('node', $node, 'field_site_url', '');
// print render($prodSiteUrl);
// specific values pulled from those fields
$chainLogoFilename = $node->field_chain_logo['und'][0]['filename'];
$chainSiteUrlRaw = $node->field_site_url['und'][0]['value'];
Getting a single value from the entity array
In the below code the $delta is set by the number in the $field_body_chain[0]. That would be bringing back the first item in the array, where $field_body_chain[2] would be bringing back the 3rd item.
$field_body_chain = field_get_items('node', $node->field_chain_franchise['und'][0]['entity'], 'body');
$render_body_chain = field_view_value('node', $node, 'body', $field_body_chain[0]);
print render ($render_body_chain);
Getting the entity field array and printing each item
This is useful if you are trying to print out an array of values, eg. defined via something like a list of checkboxes.
$accepted_payments = field_get_items('node', $node->field_chain_franchise['und'][0]['entity'], 'field_accepted_payments');
foreach ($accepted_payments as $key=>$value) {
$output = field_view_value('node', $node, 'field_accepted_payments', $accepted_payments[$key], array());
print render($output);
}
Removing html tags from the entity values
If you're rendering a list of taxonomy terms they appear as a list of links. In order to remove those links you can use the PHP strip_tags function.
PS. I'm sure there is a better way to do this so please let me know if you know what that is!
print strip_tags(render($output))
strip_tags -- 这个倒是有一定的实际作用。有时当用field_view_field时,表现形式,也可以在后台的显示模式里修改。
----------------
比如对于图片字段,默认的label隐藏,
使用medium的imagestyle来显示图片,则可以使用如下的代码:
$node = node_load(NID);
$display = array(
'label' => 'hidden',
'settings' => array(
'image_style' => 'food_pairing',
)
);
$field = field_view_field('node', $node, 'field_image', $display);
---------------------
对于body字段,打印起trim的值:
$display = array(
'label'=>'hidden',
'type' => 'text_summary_or_trimmed',
'settings'=>array('trim_length' => 150),
);
$output = field_view_field('node', $node, 'body', $display);
print render($output);
=========================