Drupal глазами разработчика

Title++, или невероятные приключения заголовка

Вот наваял тут небольшую функцию. Делает она примерно следующее:
если текущая страница не является нодой, то cool_title(0) выводит заголовок страницы, а cool_title(1) остается пустым. Если текущая страница — нода, то в cool_title(0) выводится термин, к которому отнесена нода, а в cool_title(1) выводится заголовок ноды.
<span style="color: #000000; font-weight: bold;">function</span> cool_title<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$op</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
   <span style="color: #0000ff;">$sql</span>=<a href="http://api.drupal.ru/api/function/db_fetch_array/6"   rel="nofollow" ><span style="color: #000066;">db_fetch_array</span></a><span style="color: #66cc66;">(</span><a href="http://api.drupal.ru/api/function/db_query/6"   rel="nofollow" ><span style="color: #000066;">db_query</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"SELECT nid FROM {node_revisions } WHERE title='%s'"</span>,<a href="http://api.drupal.ru/api/function/drupal_get_title/6"   rel="nofollow" ><span style="color: #000066;">drupal_get_title</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
   <span style="color: #0000ff;">$terms</span> = <a href="http://api.drupal.ru/api/function/taxonomy_node_get_terms/6"   rel="nofollow" ><span style="color: #000066;">taxonomy_node_get_terms</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'nid'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span>;
   <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$terms</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$term</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
       <span style="color: #0000ff;">$t</span>=<span style="color: #0000ff;">$term</span>-<span style="color: #66cc66;">></span><span style="color: #006600;">name</span>;
   <span style="color: #66cc66;">}</span>
 <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$op</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
    <span style="color: #b1b100;">case</span> 0:
	 <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><a href="http://api.drupal.ru/api/function/drupal_is_front_page/6"   rel="nofollow" ><span style="color: #000066;">drupal_is_front_page</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
	   <span style="color: #0000ff;">$result</span>=<a href="http://api.drupal.ru/api/function/drupal_get_title/6"   rel="nofollow" ><span style="color: #000066;">drupal_get_title</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
	 <span style="color: #66cc66;">}</span>
	 <span style="color: #b1b100;">else</span><span style="color: #66cc66;">{</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #66cc66;">!</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
			<span style="color: #0000ff;">$result</span>=<a href="http://api.drupal.ru/api/function/drupal_get_title/6"   rel="nofollow" ><span style="color: #000066;">drupal_get_title</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
		<span style="color: #66cc66;">}</span>
		<span style="color: #b1b100;">else</span><span style="color: #66cc66;">{</span>
		   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #66cc66;">!</span><span style="color: #0000ff;">$t</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
		      <span style="color: #0000ff;">$result</span>=<a href="http://api.drupal.ru/api/function/drupal_get_title/6"   rel="nofollow" ><span style="color: #000066;">drupal_get_title</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
		   <span style="color: #66cc66;">}</span>
		   <span style="color: #b1b100;">else</span><span style="color: #66cc66;">{</span>
			  <span style="color: #0000ff;">$result</span>=<span style="color: #0000ff;">$t</span>;
		   <span style="color: #66cc66;">}</span>
		<span style="color: #66cc66;">}</span>
	 <span style="color: #66cc66;">}</span>
	<span style="color: #b1b100;">break</span>;
	<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>:	
	  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #66cc66;">!</span><a href="http://api.drupal.ru/api/function/drupal_is_front_page/6"   rel="nofollow" ><span style="color: #000066;">drupal_is_front_page</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">&&</span> <span style="color: #0000ff;">$t</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span> 
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'nid'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
			<span style="color: #0000ff;">$node</span>=<a href="http://api.drupal.ru/api/function/node_load/6"   rel="nofollow" ><span style="color: #000066;">node_load</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'nid'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">)</span>;
			<span style="color: #0000ff;">$result</span>=<span style="color: #0000ff;">$node</span>-<span style="color: #66cc66;">></span><span style="color: #006600;">title</span>;
		<span style="color: #66cc66;">}</span>
	  <span style="color: #66cc66;">}</span>
	<span style="color: #b1b100;">break</span>;	
 <span style="color: #66cc66;">}</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$result</span>;
<span style="color: #66cc66;">}</span>

Доработки и критика ожидаются и приветствуются.
Пример использования

0 комментариев

Только зарегистрированные и авторизованные пользователи могут оставлять комментарии.