Drupal-Tip 1: Block Visibility für Fortgeschrittene

miss-visibility.jpg

Wenn man mit Blöcken in Drupal arbeitet (besonders wenn diese Blöcke Menüs enthalten) läuft man ihr früher oder später über den Weg: Miss Visibility. Der Block muss auf den richtigen Seiten angezeigt werden. Eine schöne Einführung gibts dazu von Michelle: Overwiew Approach to Block Visibility

Aber ach: viele Fälle sind gar nicht erfasst. Ich möchte hier nur einen behandeln, der aber extrem mächtig ist.

Normalerweise hat ein Block nicht Zugriff auf alle Variablen, die in einem Node vorkommen (wer sich unsicher ist, was für Variablen überhaupt da sind: Devel installieren und auf den "Dev_load" - Tab klicken). Supercool, alles da: Translation, Taxonomy Id usw. Wie kann ich jetzt vom Block darauf zugreifen?

Wo sind hier meine Variablen?

Der Trick ist, dass man die Variablen erst verfügbar machen muss. Dies geht mit dem Folgendem, was auf der Handbuchseite von Michelle auch vorhanden ist:

<span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);">$match </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">FALSE</span><span style="color: rgb(0, 119, 0);">;<br /> </span><span style="color: rgb(0, 0, 187);">$types </span><span style="color: rgb(0, 119, 0);">= array(</span><span style="color: rgb(221, 0, 0);">'story' </span><span style="color: rgb(0, 119, 0);">=&gt; </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">, </span><span style="color: rgb(221, 0, 0);">'page' </span><span style="color: rgb(0, 119, 0);">=&gt; </span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">);<br /> if (</span><span style="color: rgb(0, 0, 187);">arg</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">) == </span><span style="color: rgb(221, 0, 0);">'node' </span><span style="color: rgb(0, 119, 0);">&amp;&amp; </span><span style="color: rgb(0, 0, 187);">is_numeric</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">arg</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">))) {<br /> &#160; </span><span style="color: rgb(0, 0, 187);">$nid </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">arg</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">);<br /> &#160; </span><span style="color: rgb(0, 0, 187);">$node </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">node_load</span><span style="color: rgb(0, 119, 0);">(array(</span><span style="color: rgb(221, 0, 0);">'nid' </span><span style="color: rgb(0, 119, 0);">=&gt; </span><span style="color: rgb(0, 0, 187);">$nid</span><span style="color: rgb(0, 119, 0);">));<br /> &#160; </span><span style="color: rgb(0, 0, 187);">$type </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$node</span><span style="color: rgb(0, 119, 0);">-&gt;</span><span style="color: rgb(0, 0, 187);">type</span><span style="color: rgb(0, 119, 0);">;<br /> &#160; if (isset(</span><span style="color: rgb(0, 0, 187);">$types</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(0, 0, 187);">$type</span><span style="color: rgb(0, 119, 0);">])) {<br /> &#160;&#160;&#160; </span><span style="color: rgb(0, 0, 187);">$match </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">TRUE</span><span style="color: rgb(0, 119, 0);">;<br /> &#160; }<br /> }</span></span>

Der wichtige Teil hierbei ist:

<span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 119, 0);">if (</span><span style="color: rgb(0, 0, 187);">arg</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">) == </span><span style="color: rgb(221, 0, 0);">'node' </span><span style="color: rgb(0, 119, 0);">&amp;&amp; </span><span style="color: rgb(0, 0, 187);">is_numeric</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">arg</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">))) {<br /> &#160; </span><span style="color: rgb(0, 0, 187);">$nid </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">arg</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">);<br /> &#160; </span><span style="color: rgb(0, 0, 187);">$node </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">node_load</span><span style="color: rgb(0, 119, 0);">(array(</span><span style="color: rgb(221, 0, 0);">'nid' </span><span style="color: rgb(0, 119, 0);">=&gt; </span><span style="color: rgb(0, 0, 187);">$nid</span><span style="color: rgb(0, 119, 0);">));</span></span>

Es wird überprüft, ob die Seite ein Node ist, und ob sie eine Nodenummer hat, und dann ein node_load durchgeführt. Nun kann selbst ein ungeübter Programmierer mit einigem Gefummel alle im Node vorhandenen Objekte dazu nutzen, die Anzeige seines Blockes zu steuern. Ein kleiner Tip dabei noch: man möchte vielleicht, dass man manche Bedinungen verknüft. Das geht mit AND (&&) oder OR (||) oder eben den ganzen anderen logischen Verknüpfungen.

Verknüpfung von Bedingungen

Hier ein Beispiel:

$match = FALSE;
$types = array('product' => 1);
$term_id_to_trigger_show_block = 1; // replace '21' your term id.
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load(array('nid' => $nid));
  $type = $node->type;
  $terms = taxonomy_node_get_terms(arg(1));
    foreach($terms as $term) {
      if ($term->tid == $term_id_to_trigger_show_block) {
         $make_block_visible = yes;
      }
    }
  if ((!isset($types[$type]))OR((isset($types[$type]))AND($make_block_visible == yes))) {
    $match = TRUE;
  }
}
return $match;
 

Hier wird der Block NICHT angezeigt, wenn er vom content type "product ist", aber er wird angezeigt, wenn es ein "product" ist und dazu einem bestimmten Taxonomy Term zugeordnet ist. Lässt sich alles mit Logik lösen ;)