[Решено] Как в div поля ноды добавить класс - счётчик с количеством значений?

Главные вкладки

Комментарии

Аватар пользователя DD 85 DD 85 13 июля 2014 в 23:35

"ХулиGUN" wrote:
theme_field()

Круто, но непонятно. Если таким образом, то мне нужны более детальные подсказки.

Вообще-то у меня установлен Field Formatter Class, но столкнулся с тем, что нет соответствующего токена. Если б найти или реализовать такой токен, то для меня это был бы лучший вариант.

Аватар пользователя DD 85 DD 85 13 июля 2014 в 23:55

"ХулиGUN" wrote:
https://api.drupal.org/api/drupal/modules!field!field.module/function/th...
С этой страницей я знаком. Только что ещё раз перечитал.

"ХулиGUN" wrote:
считаете количество элементов и выводите соответственно класс

В этом и весь вопрос? Если б я знал, как это сделать?

Аватар пользователя DD 85 DD 85 14 июля 2014 в 1:18

Если несложно, пожалуйста, подскажите, куда это здесь вставить?

<?php
function theme_field($variables) {
  
$output '';

  

// Render the label, if it's not hidden.
  
if (!$variables['label_hidden']) {
    
$output .= '<div class="field-label"' $variables['title_attributes'] . '>' $variables['label'] . ':&nbsp;</div>';
  }

  

// Render the items.
  
$output .= '<div class="field-items"' $variables['content_attributes'] . '>';
  foreach (
$variables['items'] as $delta => $item) {
    
$classes 'field-item ' . ($delta 'odd' 'even');
    
$output .= '<div class="' $classes '"' $variables['item_attributes'][$delta] . '>' drupal_render($item) . '</div>';
  }
  
$output .= '</div>';

  

// Render the top-level DIV.
  
$output '<div class="' $variables['classes'] . '"' $variables['attributes'] . '>' $output '</div>';

  return 

$output;
}
?>

Чтобы получилось:

<div class="field field-name-field-image field-type-image field-label-hidden 1">
        <div class="field-items">
                <div class="field-item even">
                        <img width="" height="" alt="" src="xxx.jpg" typeof="foaf:Image">
                </div>
        </div>
</div>
Аватар пользователя DD 85 DD 85 15 июля 2014 в 0:40

Ну что ж. Денису, оказалось, сложно дать более подробную подсказку. Впрочем, и на этом спасибо!
Для потомков оставляю код решения.
На всякий случай, если Вы не знаете даже того, куда его добавить, то в конец template.php лежащего в Вашей теме. Заменив theme названием Вашей темы.

<?php
function theme_field($variables) {
  
$output '';

  

// Render the label, if it's not hidden.
  
if (!$variables['label_hidden']) {
    
$output .= '<div class="field-label"' $variables['title_attributes'] . '>' $variables['label'] . ':&nbsp;</div>';
  }

  

// Render the items.
  
$output .= '<div class="field-items"' $variables['content_attributes'] . '>';
  foreach (
$variables['items'] as $delta => $item) {
    
$classes 'field-item ' . ($delta 'odd' 'even');
    
$output .= '<div class="' $classes '"' $variables['item_attributes'][$delta] . '>' drupal_render($item) . '</div>';
  }
  
$output .= '</div>';

  

// Render the top-level DIV.
  
$output '<div class="' $variables['classes'] . ' ' count($variables['items']) . '"' $variables['attributes'] . '>' $output '</div>';

  return 

$output;
}
?>
Аватар пользователя DD 85 DD 85 15 июля 2014 в 14:20

"ХулиGUN" wrote:
Чтобы ограничиться конкретным полем нужно передать его machine_name в название метода
theme_field__machine_name()

А если для двух полей? Придётся всё это два раза дублировать или можно как-нибудь сразу два указать?