Skip to main content

Is it correntcorrect to make queries to DB in preprocess functions?

added 27 characters in body
Source Link
kalabro
  • 7.9k
  • 35
  • 46

I've just started using Drupal after long experience of using other framework. When I do something new, I'm scaring to crash performance because of my incomprehension. So, my problem: I want to add dynamically some data from database to some existing page region. For example, I want to add list of child terms to taxonomy term page. And I don't need to change any other things on this page, so I don't want to turn on View. Is it normal to query data in preprocess functions? My solution for D7 was like this:

function mymodule_taxonomy_term_view_alter(&$build) {
  if ($build['#view_mode'] == 'full') {
    $children = taxonomy_get_tree($build['#term']->vid, $build['#term']->tid, 1);
    foreach($children as $term) {
      $build['children'][] = (array)$term;
    }
  }
}

function mymodule_preprocess_taxonomy_term(&$variables) {
  if($variables['vocabulary_machine_name'] == 'catalog') {
    
    if(count($variables['elements']['children'])) {
      $items = array();
      foreach($variables['elements']['children'] as $term) {
        $items[] = array(
          'data' => l($term['name'], 'taxonomy/term/' . $term['tid'])
        );
      }
      
      $variables['content']['children']['#markup'] = theme('item_list', array('items' => $items, 'title' => 'Text terms list', 'type' => 'ul', 'attributes' => array()));
    }
  }
}
function mymodule_taxonomy_term_view_alter(&$build) {
  if ($build['#view_mode'] == 'full') {
    $children = taxonomy_get_tree($build['#term']->vid, $build['#term']->tid, 1);
    foreach($children as $term) {
      $build['children'][] = (array)$term;
    }
  }
}

function mymodule_preprocess_taxonomy_term(&$variables) {
  if($variables['vocabulary_machine_name'] == 'catalog') {
    
    if(count($variables['elements']['children'])) {
      $items = array();
      foreach($variables['elements']['children'] as $term) {
        $items[] = array(
          'data' => l($term['name'], 'taxonomy/term/' . $term['tid'])
        );
      }
      
      $variables['content']['children']['#markup'] = theme('item_list', array('items' => $items, 'title' => 'Text terms list', 'type' => 'ul', 'attributes' => array()));
    }
  }
}

Any comment are welcome.

I've just started using Drupal after long experience of using other framework. When I do something new, I'm scaring to crash performance because of my incomprehension. So, my problem: I want to add dynamically some data from database to some existing page region. For example, I want to add list of child terms to taxonomy term page. And I don't need to change any other things on this page, so I don't want to turn on View. Is it normal to query data in preprocess functions? My solution for D7 was like this:

function mymodule_taxonomy_term_view_alter(&$build) {
  if ($build['#view_mode'] == 'full') {
    $children = taxonomy_get_tree($build['#term']->vid, $build['#term']->tid, 1);
    foreach($children as $term) {
      $build['children'][] = (array)$term;
    }
  }
}

function mymodule_preprocess_taxonomy_term(&$variables) {
  if($variables['vocabulary_machine_name'] == 'catalog') {
    
    if(count($variables['elements']['children'])) {
      $items = array();
      foreach($variables['elements']['children'] as $term) {
        $items[] = array(
          'data' => l($term['name'], 'taxonomy/term/' . $term['tid'])
        );
      }
      
      $variables['content']['children']['#markup'] = theme('item_list', array('items' => $items, 'title' => 'Text terms list', 'type' => 'ul', 'attributes' => array()));
    }
  }
}

Any comment are welcome.

I've just started using Drupal after long experience of using other framework. When I do something new, I'm scaring to crash performance because of my incomprehension. So, my problem: I want to add dynamically some data from database to some existing page region. For example, I want to add list of child terms to taxonomy term page. And I don't need to change any other things on this page, so I don't want to turn on View. Is it normal to query data in preprocess functions? My solution for D7 was like this:

function mymodule_taxonomy_term_view_alter(&$build) {
  if ($build['#view_mode'] == 'full') {
    $children = taxonomy_get_tree($build['#term']->vid, $build['#term']->tid, 1);
    foreach($children as $term) {
      $build['children'][] = (array)$term;
    }
  }
}

function mymodule_preprocess_taxonomy_term(&$variables) {
  if($variables['vocabulary_machine_name'] == 'catalog') {
    
    if(count($variables['elements']['children'])) {
      $items = array();
      foreach($variables['elements']['children'] as $term) {
        $items[] = array(
          'data' => l($term['name'], 'taxonomy/term/' . $term['tid'])
        );
      }
      
      $variables['content']['children']['#markup'] = theme('item_list', array('items' => $items, 'title' => 'Text terms list', 'type' => 'ul', 'attributes' => array()));
    }
  }
}

Any comment are welcome.

new tags added
Link
kalabro
  • 7.9k
  • 35
  • 46
Tweeted twitter.com/#!/StackDrupal/status/101966223109136384
Source Link
kalabro
  • 7.9k
  • 35
  • 46
Loading