web-dev-qa-db-ja.com

分類は、いくつかのカテゴリタイプと一致していますか?

D7がいくつかの分類フィールドをインラインで表示し、それらの間にセパレータを配置することは可能ですか? 2つのカテゴリタイプを持つノード、たとえば「年」と「歌手」のあるノードが欲しいです。これで、分類が列に表示されます。

Year: 1999
Singer: Britney Spears

そして、私はそれをいくつかのセパレータでインラインに表示したいです:

Year: 1999 |separator| Singer: Britney Spears

私は 同様の質問 を見つけましたが、1つの複数値分類フィールドを表示することを扱います。それは私が望むものではありません。では、いくつかのタイプの分類フィールドをインラインで表示するには、どこを調べ、何をオーバーライドする必要がありますか?

Update:2日間費やした後、node.tpl.phpとfield.tpl.phpを使用して実行しました。セパレータを完全に印刷することを常にお勧めします。 CSSに依存することは良いことですが、IE6を使用している人がいる場合はそうではありません(2011年まで見たことがあります)。ここでこれまでで最高の解決策を講じたわけではありませんが(コードが粗末です)、それでも機能し、少なくとも私が望んでいることを実行します。私は次のことをしました:

1)node.tpl.phpをmodules\nodeからsites\all\themes\mythemeにコピーしました。 2)$ content変数を使用して、すべての分類フィールドを検索します。

$taxonomy_field_names = array();
$all_field_names = array_keys($content);
foreach($all_field_names as $i => $value)
{
    $field_name = $all_field_names[$i];
    $field_type_name = '#field_type';

    //if content contains an array with $field_name and it is an array itself
    //and has an item #field_type of taxonomy_term_reference
    //it is a taxonomy field
    if(is_array($content[$field_name]) &&
        array_key_exists($field_type_name, $content[$field_name]) &&
        $content[$field_name][$field_type_name] == 'taxonomy_term_reference')
    {
        $taxonomy_field_names[] = $all_field_names[$i];
    }
} unset($i);

3)レンダリングを防ぐためにそれらを隠しました:

//hiding taxonomy fields
$count_taxonomy_fields = count($taxonomy_field_names);
for($i = 0; $i < $count_taxonomy_fields; $i++)
{
    hide($content[$taxonomy_field_names[$i]]);
}

4)$ contentをレンダリングした後、分類フィールドをレンダリングしました。

//rendering taxonomy fields
for($i = 0; $i < $count_taxonomy_fields; $i++)
{
    //printing a separator after each of the fields except the very last
    if($i < $count_taxonomy_fields - 1)
    {
        $content[$taxonomy_field_names[$i]]['#separator'] = '&bull;';
    }
    print render($content[$taxonomy_field_names[$i]]);
}

5)fields.tpl.phpをmodules\fieldからsites\all\themes\mythemeにコピーし、名前をfield--taxonomy-term-reference.tpl.phpに変更しました(すべてに影響します)。前のステップで$ content [$ taxonomy_field_names [$ i]] ['#separator']を設定したら、field--taxonomy-term-reference.tpl.phpで$ element ['#separator']として使用できます。

<?php if (isset($element['#separator'])): ?>
<span class="field-item-separator"><?php print $element['#separator']?></span>
<?php endif; ?>

6)次の行をsites\all\themes\mytheme\css\fields.cssに追加しました:

.field-type-taxonomy-term-reference .field-item {
   float:left;
}

私の質問に答えてくれた人々に心から感謝します。

3
Ari Linn

オプション1:純粋なCSS。コンテンツタイプの表示オプションがすべての分類基準フィールドが互いに隣り合うように設定されていると仮定すると、各分類フィールドをインライン要素としてラップする各divを表示し、次に +セレクター および :before 疑似要素を使用して各フィールド間にセパレーターを追加します。

これらの2つの特別なセレクターは、ほとんどのWebブラウザーでサポートされていますが、 古いブラウザーで使用できるかどうかを確認する サイトでサポートする必要がある場合があります。

たとえば、各分類フィールドが「field-type-taxonomy-reference」クラスのdivでラップされ、ノードに「node-type」クラスのdivがラップされている場合、CSSは次のようになります。

//make the taxonomy fields and their contents display as inline elements
.node-type .field-taxonomy-reference,
.node-type .field-taxonomy-reference * {
  display: inline;
}

//add a pipe separator between two taxonomy fields
.node-type .field-taxonomy-reference + .field-taxonomy-reference:before {
  display: inline;
  content: " | ";
}

オプション2:カスタムフィールドテンプレートを使用してHTMLを編集します。field.tpl.php ファイルをDrupalコアをテーマに追加し、フィールド名を「taxonomy-reference.tpl.php」に変更します。これは、サイト全体のすべての分類参照フィールドに影響を与えるため、望ましくない場合があります。 template_preprocess_field() をテーマのtemplate.phpに実装して、このテンプレートの影響を受けるノード(およびフィールド)のみに新しいテンプレートの提案を追加することもできます。例えば:

function YOURTHEME_preprocess_field(&$vars) {
  $variables['theme_hook_suggestions'] = array('field__' . $element['#field_type'] . '__' . $element['#bundle']);
}

これにより、field--taxonomy-reference--news-article.tpl.phpという名前のフィールドテンプレートを使用して、「new article」というノードタイプのすべての分類基準参照フィールドに影響を与えることができます。 template_preprocess_field() のドキュメントページの1つに、チェックできる他のいくつかの変数(フィールド名など)があります。次のように、複数のノードタイプで異なる名前(およびタイプ)を持つ複数のフィールドが同じフィールドテンプレートを使用するようにすることができます。

function YOURTHEME_preprocess_field(&$vars) {
  $field_names = array('field_name_a', 'field_name_b', 'field_name_c');

  if(in_array($element['#field_name'], $field_names) {
    $variables['theme_hook_suggestions'] = array('field__inline-style');
  }
}

これにより、「field_name_a」、「field_name_b」などのフィールドに「field--inline-style.tpl.php」というテンプレートを使用できます。

4
sheena_d

これにはいくつかの異なるオプションがあります。

オプション1:CSSおよびNodeテンプレート-CSSを使用してフィールドをインラインで表示できます(例:floatまたはdisplay:inline-blockを使用)。次に、特定のテンプレートファイルを作成できます。ノードタイプと2つのフィールド間のセパレータを出力します。

オプション2:フィールドとNodeテンプレート-質問でリンクした回答に記載されているアプローチを使用して両方のフィールドに適用し、インラインで表示することもできます。次にテンプレートでノードタイプに固有のファイルで、区切り文字を挟んで両方のフィールドを印刷できます。

オプション3:カスタムテーマ関数を使用する-ノードタイプまたはフィールドのいずれかにカスタムテーマ関数を使用して同じことを実行し、フィールドをインラインで表示し、その間にセパレーターを印刷することもできます。

私はあなたにとって最も簡単に聞こえる方法を使うことを勧めます。多くの場合、人々はある方法で、同じように正しい方法で問題に取り組む方が快適です。

0
Chaulky

区切り文字には、少なくともより簡単な解決策があります:分類法フォーマッターモジュール: http://drupal.org/project/taxonomy_formatter

しかし、ここに、theme.phpファイルに追加するいくつかのテーマオーバーライドコードがあります。

function NAMEOFYOURTHEME_field__taxonomy_term_reference($vars) {
  $output = '';

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

  // Render the items.
  $output .= '<ul class="field-items"' . $vars['content_attributes'] . '>';
  foreach ($vars['items'] as $delta => $item) {

    // Set a delimiter, in this case a comma
    $delimiter = ' | ';

    // If the item is the last in the array remove the comma
    if (end($vars['items']) === $item) {
      $delimiter = '';
    }

    $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');

    // Output each taxonomy term item, with the delimiter on the end
    $output .= '<li title="Term Description should show here...themephp"  class="' . $classes . '"' . $vars['item_attributes'][$delta] . '>' . drupal_render($item) . $delimiter . '</li>';
  }

  $output .= '</ul>';
  // Render the top-level wrapper element.
  $tag = $vars['label_hidden'] ? 'div' : 'section';
  $output = "<$tag class=\"" . $vars['classes'] . '"' . $vars['attributes'] . '>' . $output . "</$tag>";

  return $output;
}
?>

私はそのコードの作成者ではありませんが(PHPプログラマーではありません))、同様の解決策を探す際に見つけました。

私の場合、用語のホバーに表示する分類用語の説明が必要です-つまり、単にTITLE属性で。これは、ノード表示とノードフォームの両方に必要です。

誰かが私を手伝ってくれるなら、私はとても感謝しています。私は何週間も研究、実験、投稿してきました!

0
Greta