Funkce vzhledu

Všechny funkce začínající slovem theme (např. theme_username) vytvářejí HTML výstup a je možné je upravit pomocí souboru template.php.

Obecný posutp

  1. Najděte požadovanou funkci (přímo v Drupalu nebo na api.drupal.org).
  2. Zkopírujte funci do template.php, přejmenujte její název např. z theme_... nebo template_... nejlépe na název používaného tématu MYTHEMENAME_..., což je zhlediska výkonu nejlepší a upravte dle libosti.
  3. Pro zjištění hodnot pole je vhodné do funkce vložit např. funkci var_dump() nebo kpr() s požadovaným polem
    • var_dump($variables);
      kpr($variables);
  4. Po úpravách template.php je důležité obnovit cache tématu vzhledu!
    • Přejděte na stránku s tématy vzhledu a aktuální stav uložte.

Příklad: Úprava délky zobrazovaného uživatelského jména

Drupal 7:

  1. Funkce, která mimo jiné ořezává délku uživatelského jména se jmenuje template_preprocess_username.
  2. Tuto funkci je vhodé doplnit ještě o možnost zobrazení úplného jména pomocí modulu RealName a nově vytvořeného pole field_fullname.
  3. Výsledná funkce může vypadat třeba takto
    • /**
       * Implements hook_preprocess_username().
      */
      function MYTHEMENAME_preprocess_username(&$variables) {
      $account = $variables['account'];

      $variables['extra'] = '';
      if (empty($account->uid)) {
      $variables['uid'] = 0;
      if (theme_get_setting('toggle_comment_user_verification')) {
      $variables['extra'] = ' (' . t('not verified') . ')';
      }
      }
      else {
      $variables['uid'] = (int) $account->uid;
      $user = user_load($account->uid);
      if (isset($user->field_fullname['und'][0]['value'])) {
      $name = $variables['name_raw'] = $user->field_fullname ['und'][0]['safe_value'];
      } else {
      // Set the name to a formatted name that is safe for printing and
      // that won't break tables by being too long. Keep an unshortened,
      // unsanitized version, in case other preprocess functions want to implement
      // their own shortening logic or add markup. If they do so, they must ensure
      // that $variables['name'] is safe for printing.
      $name = $variables['name_raw'] = format_username($account);
      }
      }

      if (drupal_strlen($name) > 20) {
      //$name = drupal_substr($name, 0, 15) . '...';
      }
      $variables['name'] = check_plain($name);

      $variables['profile_access'] = user_access('access user profiles');
      $variables['link_attributes'] = array();
      // Populate link path and attributes if appropriate.
      if ($variables['uid'] && $variables['profile_access']) {
      // We are linking to a local user.
      $variables['link_attributes'] = array('title' => t('View user profile.'));
      $variables['link_path'] = 'user/' . $variables['uid'];
      }
      elseif (!empty($account->homepage)) {
      // Like the 'class' attribute, the 'rel' attribute can hold a
      // space-separated set of values, so initialize it as an array to make it
      // easier for other preprocess functions to append to it.
      $variables['link_attributes'] = array('rel' => array('nofollow'));
      $variables['link_path'] = $account->homepage;
      $variables['homepage'] = $account->homepage;
      }
      // We do not want the l() function to check_plain() a second time.
      $variables['link_options']['html'] = TRUE;
      // Set a default class.
      $variables['attributes_array'] = array('class' => array('username'));
      }

Drupal 6:

  1. Funkce, která mimo jiné ořezává délku uživatelského jména je v souboru includes/thhme.inc a jmenuje se theme_username.
  2. Funkci zkopírujte do template.php, přejmenujte její název z theme_username na název vašeho tématu MYTHEMENAME_username a upravte požadovanou část jejího kódu:
    function theme_username($object) {
      if ($object->uid && $object->name) {
        // Shorten the name when it is too long or it will break many tables.
        if (drupal_strlen($object->name) > 20) {
          $name = drupal_substr($object->name, 0, 15) .'...';
        }
    ...

    např. na:

    function MYTHEMENAME_username($object) {
      if ($object->uid && $object->name) {
        // Shorten the name when it is too long or it will break many tables.
        if (drupal_strlen($object->name) > 30) {
          $name = drupal_substr($object->name, 0, 25) .'...';
        }
    ...

    Místo phptemplate je z hlediska výkonu vhodnější psát název tématu. 

  3. Přejděte na stránku s tématy vzhledu a aktální stav uložte.