Quick tip: howto modify a record label – label_callback | qzminski Blog

Quick tip: howto modify a record label – label_callback

Let’s assume we want to display the number of cds in each category in our CD collection. To do that, we could create an additional field in the table, which would store the information. That’s a good idea because of a better performance (and we will take a look at it in the future), but for now I want to show you how to use a label_callback.

We would like to add the number of CDs in each category, so open the dca/tl_dca_category.php file. Find the label array which is responsible for displaying a label:

// List
'list' => array
(
  'sorting' => array
  (
    'mode'                    => 1,
    'fields'                  => array('title'),
    'flag'                    => 1,
    'panelLayout'             => 'filter;search,limit'
  ),
  'label' => array
  (
    'fields'                  => array('title'),
    'format'                  => '%s',
    'label_callback'          => array('tl_cds_category', 'addCdsNumber')
  ),
  'global_operations' => array
  (

Insert a new element of the array with key label_callback. As a value it takes an array, which first element is a class and the second is a function.

Now scroll down to the end of dca file, and just after the main array ends, insert:

); // end of the main array $GLOBALS['TL_DCA']['tl_cds_category']

class tl_cds_category extends Backend {

  /**
   * Adds a number for cds
   * @param array
   * @param string
   * @return string
   */
  public function addCdsNumber($row, $label)
  {
    // ...
  }

}

?>

As the first param, function takes an array containing all the fields in a database table tl_cds_category. We can spy the array by inserting following code:

return print_r($row);

The $label variable contains a string built using config from $GLOBALS['TL_DCA']['tl_cds_category']['list']['label'].

Okay, let’s fetch the number of children from a database. That is why we extended our class with Backend – to gain access to $this->Database methods.

Modify our function as follows:

public function addCdsNumber($row, $label)
{
  $objChildren = $this->Database->prepare("SELECT COUNT(*) AS count FROM tl_cds WHERE pid=?")->execute($row['id']);

  $label .= ' <span style="color:#b3b3b3; padding-left:3px;">' . sprintf('[%s cds]', $objChildren->count) . '</span>';
  return $label;
}

Fristly, we execute a simple query that counts all cds that belong to the current category. Then we append fetched data to the label. At the end we simply return modified string. I hope this is clear for you.

Now go to your backend and check out the changes!


About Author

Kamil Kuzminski

Hi! I'm a webdeveloper from Olsztyn, Poland. I'm the manager of Contao (fka TYPOlight) polish support website and community. I work mainly as a freelancer for private clients or various agencies.





Comments

  1. Lionel May 16th

    Comment Arrow

    Good idea !

    thx


  2. Kamil Kuzminski May 16th

    Comment Arrow

    Thanks for your comment, glad to know that someone reads my blog :)


  3. NguyenSon October 4th

    Comment Arrow

    you save my life


Add Yours

  • Author Avatar

    YOU


Comment Arrow