Созонов Алексей

Веб-разработчик

Правильный вывод полей в Drupal

09.08.2015

В нужном месте в файле .tpl прописываем

$node = node_load($nid);
$field = field_get_items('node', $node, 'field_name');
$output = field_view_value('node', $node, 'field_name', $field[$delta]);
$node = node_load($nid);
$image = field_get_items('node', $node, 'field_image');
$output = field_view_value('node', $node, 'field_image', $image[0], array(
        'type' => 'image',
        'settings' => array(
        'image_style' => 'thumbnail',
        'image_link' => 'content',
    ),
));

Если у нас поле с неограниченным количеством изображений

<?php
$node = node_load($nid);
$image = field_get_items('node', $node, 'field_NAME');
foreach ($image as $key=>$value) {
$output = field_view_value('node', $node, 'field_NAME', $image[$key], array(
  'type' => 'image',
  'settings' => array(
    'image_style' => 'thumbnail', //place your image style here
    'image_link' => 'content',
  ),
));
print render($output);
}

Как делать НЕ НАДО!

$block['content'] = $node->field_name['und'][0]['safe_value'];

Еще для изображений

If you wanted to use the thumbnail of an image you could do it in this way (in Drupal 7):

  1. In admin menu > Structure > Content Type > Choose the content type you need > Manage Display
  2. Choose the display you need in the upper right corner - teaser or default
  3. Make sure that the image field you need is not hidden otherwise you will not see it in the $content array. Also change the image field properties to thumbnail
  4. Go to your specific content type template page (i.e. for article content type go to your template directory/templates/node--article.tpl.php) or create it if it does not exists by copying it from one of the core templates (i.e. your site's directory/themes/bartik/node.tpl.php) and rename it as you need
  5. Use print render($content['your image field']); to show it where you want in the template
  6. Use hide($content['your image field']); not to show it with the other content

Вывод полей терминов таксономии:

<?php
    $term=taxonomy_term_load($node->field_tags['und'][0]['tid']);
    $result=field_view_field('taxonomy_term',$term,'field_image');
    echo render($result);
?>

Вывод полей пользователя:

<?php
  global $user;
  $user_data = user_load($user->uid);

  $field = field_get_items('user', $user_data, 'field_user');
  $output = field_view_value('user', $user_data, 'field_user', $field[0]);
  echo render($output);

  // Вывод аватара пользователя на странице материала
  print drupal_render(field_view_field('user',$user_data,'field_userphoto',array('label' => 'hidden', 'settings' => array('image_style' => 'post_avatar'))));

#Drupal