4

I am trying to access a static method of a controller class from a module, but my code doesn't work.

This is the controller class.

namespace Drupal\content_statistics\Controller;

use Drupal\Core\Controller\ControllerBase;

class ContentStatisticsCount extends ControllerBase {

  public static contentStatisticsCount() {
    $value = $this->t('hi');
    return $value;
  }

}

This is the module code.

use Drupal\Core\Controller\ControllerBase;
use Drupal\content_statistics\Controller;

/**
 * Implements hook_node_view().
 */ 
function content_statistics_node_view($node, $display, $view_mode, $langcode) {
  $value = ContentStatisticsCount::contentStatisticsCount();
  print_r($value);
  die;
}

This is giving me the following PHP error:

PHP Fatal error: Class 'ContentStatisticsCount' not found in /home/qbuser/Projects/test/modules/custom/content_statistics/content_statistics.module on line 15

  • Drupal\content_statistics\Controller\ContentStatisticsCount::contentStatisticsCount(); also if it is a static method, don' use $this in it(even if t() is from a trait). – user21641 Feb 8 '16 at 7:39
  • 1
    You don't import full namespaces in PHP, rather specific classes/functions/etc. So: use Drupal\content_statistics\Controller; needs to be use Drupal\content_statistics\Controller\ContentStatisticsCount; – Clive Feb 8 '16 at 11:19
  • 1
    On a slightly different note; using a method like this in a controller class seems like a bit of a code smell. – Chapabu Feb 8 '16 at 17:43
9

If you want to define a controller, then don't use a static method:

namespace Drupal\content_statistics\Controller;

use Drupal\Core\Controller\ControllerBase;

class ContentStatisticsController extends ControllerBase {

  public function count() {
    $value = $this->t('hi');
    return $value;
  }

}

The use statement should import the class, not the namespace. But this is not the only problem.

If you want to use a controller class in a hook, create an instance of the controller and use the method of that instance:

use Drupal\content_statistics\Controller\ContentStatisticsController

$statistics = new ContentStatisticsController;
$value = $statistics->count();

Your class and method had the same name, I split the name in a controller class name and the method count().

  • The OP's problem is getting the error about the class not being found. it could be unappropriated to add a static method to a controller, but that is not causing the error the OP sees. – kiamlaluno Feb 8 '16 at 17:47
  • $this->t() will not work in a static method, this should then be static too. But why use the controller base class in first place, when it is used only for a static method? – 4k4 Feb 8 '16 at 18:03
  • I agree the code shown by the OP doesn't make sense. The answer should first answer about the error the OP gets, and then point out what else is wrong in the code shown by the OP, including the fact a controller should not be used as helper class for static methods only. – kiamlaluno Feb 9 '16 at 6:39
  • I edited the answer to address all problems to get a working solution. – 4k4 Feb 9 '16 at 9:37
  • 1
    @lordZ3d, for more advanced cases see drupal.stackexchange.com/questions/245735/… – 4k4 Feb 26 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.