オリジナルのthemez関数の定義についての質問

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
ken8ken8ken88's picture

kyokoさんの紹介で参加させて頂ましたidaです。
現在drupalを使いアメリカンフットボールのCSMを作成しようと考えているのですが、drupalを使い出し一ヶ月も経たない素人で毎日苦戦しています。

現在ドロップダウンメニューを作成しようとしているのですが、HTM出力関数が自分の納得いかないものであり新たに関数を定義したいと考えています。
しかしながら現在のHTML関数はそのまま残しておきたく(他の場所で関数が使用されているため。)新たに自作の関数を定義したいと思っています。
色々調べた結果template.phpファイルに関数の定義をできると知り自分の出力してほしい関数『theme_spry()』と言う名の関数を定義したのですが、実行してくれません。
キャッシュのクリアーなどは行っているのですが、どうやら新しいtheme関数を定義した後hook_theme()にて利用できるように定義づける必要あることを知りました。しかしながらその定義の仕方自体が理解できていなく悩み続けています。

page.tpl.phpの記載内容------------------------

<?php
if ($primary_links):
?>

<?php
print theme('links', $primary_links);
?>

<?php
endif;
?>

//----------------------------------------------

変更後のpage.tpl.phpの記載内容(関数spry)を定義した。あっているのかな?--------------

<?php
if ($primary_links):
?>

<?php
print theme('spry', $primary_links);
?>

<?php
endif;
?>

//-------------------------------------------------

template.phpの内容(引っ張ってきたlinks関数を雑に改造)

function template_spry($links, $attributes = array('class' => 'links')) {
global $language;
$output = '';
if (count($links) > 0) {
$output = '

  '; $num_links = count($links); //入れる
  $i = 1;
  foreach ($links as $key => $link) {
  $class = $key; // Add first, last and active classes to the list of links to help out themers.
  if ($i 1) { $class .= ' first'; } if ($i $num_links) {
  $class .= ' last';
  }
  if (isset($link['href']) && ($link['href'] $_GET['q'] || ($link['href'] '' && drupal_is_front_page()))
  && (empty($link['language']) || $link['language']->language == $language->language)) {
  $class .= ' active';
  }
  $output .= ' $class)) .'>';
  if (isset($link['href'])) {
  // Pass in $link as $options, they share the same keys.
  $output .= l($link['title'], $link['href'], $link);
  }
  else if (!empty($link['title'])) {
  // Some links are actually not links, but we wrap these in for adding title and class attributes
  if (empty($link['html'])) {
  $link['title'] = check_plain($link['title']);
  }
  $span_attributes = '';
  if (isset($link['attributes'])) {
  $span_attributes = drupal_attributes($link['attributes']);
  }
  $output .= ''. $link['title'] .'';
  }
  $i++;
  $output .= "\n"; }
  $output .= '

'; }
return $output;
}

//--------------------------------------------------------------------------
以上が変更し苦戦している内容です。spryよ実行してくれ.......。

最終的には、カテゴリーがドロップダウンで表示されるようなメニューを作りたいと考えています。
例として私が趣味でwordpressで作っていたメニューバーみたいな仕組みです。→http://www.hoodtimez.com
もしも何か良いモジュールがあれば紹介して頂ければ幸いです。
かっこ良く自動更新してくれるようなメニューが目標です。
長々となりましたが皆様、助言、アドバイス、指摘なんでもいいんでお返事待ってます。

Comments

はじめまして

qchan's picture

PanthersのサイトがDrupalになるんですね。楽しみだなー。
Panthersって名前になった頃からのFanなんです。関西Meetupでお会いましょう。

hookの解説はSteinがしてくれましたので、別の方法を。
Moduleで実現するとしたら「Nice menus」が人気があります。
縦、横どちらでもサブメニューが使えます。デザインは読み込むCSSを変えれば自由になります。
http://drupal.org/node/185545

Nice memusで親メニューからのリンクを停止したいときは、
http://groups.drupal.org/node/22239#comment-77848

メニューを画像と入れ替えたい時は、
http://groups.drupal.org/node/22239#comment-77971

などを見てくださいね。

xbro's picture

ken8ken8ken88さん、

ありがとう御座います。ZENに関して私もまったく同じ疑問がありました。
凄く関心がありますこのトピック。

スコット

いい質問!

setvik's picture

こんにちは、

いい質問ですね!合っています。
自分のtheme関数を作るために、その関数の名前をtemplate.phpのhook_themeに定義しなければならないです。

ken8ken8ken88さんの例で、下の関数になります。

もし、テーマの名前は "mytheme"であれば、"hook_theme()"を "mytheme_theme()の名前で定義するのです:

<?php
function mytheme_theme() {
  return array(
   
'spry' => array(),
  );
}

function
theme_spry($links, $attributes = array('class' => 'links')) {
 
// ... ken8ken8ken88さんが上に書いた関数の内容 (関数の名前を"theme_spry"に変えなければならないだけです。)
}
?>

お答えありがとうございます。

ken8ken8ken88's picture

ご指摘ありがとうございます。
僕自身zenというテーマのstartkitというものをベースに作っているのですが、ページ上の警告表示の所に以下のようなエラーが発生してしまいます。

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'panthers_spry' was given in maru ●●●/●●●/includes/theme.inc on line 617.

というエラーが出てしまします。

zenテーマの中に以下のようなtheme()関数を定義する場所が用意されていたので、setvikさんに教わった事を参考に以下のように定義したのですが何か問題あるでしょうか?ちなみに私がのテーマ名はpanthersとなっています。

//template.phpの内容------------------------------------------------------

<?php
function panthers_theme(&$existing, $type, $theme, $path) {
 
$hooks = zen_theme($existing, $type, $theme, $path);
 
// Add your theme hooks like this:
  /*
  $hooks['hook_name_here'] = array( // Details go here );
  */
  // @TODO: Needs detailed comments. Patches welcome!
 
$hooks['spry'] = array();
  return
$hooks;
}

function
theme_spry($links, $attributes = array('class' => 'links')) {
  global
$language;
 
$output = '';

  if (
count($links) > 0) {
   
$output = '<ul id="MenuBar2" class="MenuBarHorizontal">';
   
$num_links = count($links); //入れる
   
$i = 1;
    foreach (
$links as $key => $link) {
     
$class = $key;

     
// Add first, last and active classes to the list of links to help out themers.
     
if ($i == 1) {
       
$class .= ' first';
      }
      if (
$i == $num_links) {
       
$class .= ' last';
      }
      if (isset(
$link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
          && (empty(
$link['language']) || $link['language']->language == $language->language)) {
       
$class .= ' active';
      }
     
$output .= '<li'. drupal_attributes(array('class' => $class)) .'>';

      if (isset(
$link['href'])) {
       
// Pass in $link as $options, they share the same keys.
       
$output .= l($link['title'], $link['href'], $link);
      }
      else if (!empty(
$link['title'])) {
       
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
       
if (empty($link['html'])) {
         
$link['title'] = check_plain($link['title']);
        }
       
$span_attributes = '';
        if (isset(
$link['attributes'])) {
         
$span_attributes = drupal_attributes($link['attributes']);
        }
       
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
      }

     
$i++;
     
$output .= "</li>\n";
    }

   
$output .= '</ul>';
  }

  return
$output;
}
?>

ごめん、僕が間違え

setvik's picture

ごめんなさい、僕が間違えました。

モージュルにtheme関数を定義するばあいは、"theme_[function name]"を名付けるんですが、テーマの中に入れると"[theme_name]_[function name]"を名付けなければなりません。

つまり、"theme_spry"を "panthers_spry"に変えれば、動くと思います。

ありがとうございます。

ken8ken8ken88's picture

ついに問題解決しました!!
今後共頑張ってサイト完成させていくので!アドバイスよろしく御願いします。

fuji@drupal.orgさん下記がプログラム内容です。

template.phpの内容
<? php
function panthers_theme(&$existing, $type, $theme, $path) {
$hooks = zen_theme($existing, $type, $theme, $path);
// Add your theme hooks like this:
/*
$hooks['hook_name_here'] = array( // Details go here );
*/
// @TODO: Needs detailed comments. Patches welcome!
$hooks['spry'] = array();
return $hooks;
}

function panthers_spry($links, $attributes = array('class' => 'links')) {
global $language;
$output = '';

if (count($links) > 0) {
$output = '

// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ' first';
}
if ($i == $num_links) {
$class .= ' last';
}
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '' && drupal_is_front_page()))
&& (empty($link['language']) || $link['language']->language == $language->language)) {
$class .= ' active';
}
$output .= '

  • $class)) .'>';
  • if (isset($link['href'])) {
    // Pass in $link as $options, they share the same keys.
    $output .= l($link['title'], $link['href'], $link);
    }
    else if (!empty($link['title'])) {
    // Some links are actually not links, but we wrap these in for adding title and class attributes
    if (empty($link['html'])) {
    $link['title'] = check_plain($link['title']);
    }
    $span_attributes = '';
    if (isset($link['attributes'])) {
    $span_attributes = drupal_attributes($link['attributes']);
    }
    $output .= ''. $link['title'] .'';
    }

    $i++;
    $output .= "

    \n";
    }

    $output .= '

    ';
    }

    return $output;
    }

    ?>

    ありがとう御座います。

    xbro's picture

    遅れながら始めまして、

    スコットと申します。

    日本 コミュニティ: Drupal Japan User Group

    Group organizers

    Group categories

    Group notifications

    This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

    Hot content this week