1

I have issues autoloading classes found in subfolders of my module with xautoload.

my module is called myentity

Here is my hook_entity_info

function myentity_entity_info() {
    $return['myentity'] = array(
        'label' => t('My entity'),
// The entity class and controller class extend the classes provided by the
// Entity API
        'entity class' => 'Drupal/myentity/class/MyEntity',
        'controller class' => 'Drupal/myentity/controller/MyEntityController',
        'base table' => 'myentity',
        'fieldable' => TRUE,
        'entity keys' => array(
            'id' => 'me_id',
            ),
        'bundles' => array(
        'myentity' => array(
            'label' => 'myentity',
        // Information below is used by the Field UI - they "attach" themselves here and lets us
        // do the standard field management that all the core entities enjoy.
            'admin' => array(
                'path' => 'admin/structure/myentity/manage',
                'access arguments' => array('administer myentity'),
                ),
            ),
        ),
    'label callback' => 'entity_class_label',
    'uri callback' => 'entity_class_uri',
    'module' => 'myentity',
    'view modes' => array(
        'full' => array(
            'label' => t('Default'),
            'custom settings' => FALSE,
        ),
        'teaser' => array(
            'label' => t('Teaser'),
            'custom settings' => FALSE,
        ),
    ),
    'metatags' => TRUE,
);

return $return;
}

My class and controller are found in

sites\all\modules\myentity\lib\Drupal\class\MyEntity.php

with the following content

<?php

namespace Drupal\myentity\class\MyEntity;

/**
 * The class used for model entities
 */
class MyEntity extends Entity {

    public function __construct($values = array()) {
        parent::__construct($values, 'myentity');
    }

    protected function defaultLabel() {
        return $this->name;
    }

    protected function defaultUri() {
        return array('path' => 'myentity/' . $this->identifier());
    }


}

and

sites\all\modules\myentity\lib\Drupal\controller\MyEntityController.php
9
  • I may be wrong as I don't know what xautoload does, but composer is PSR which means it autoloads based upon class name -> path. So if you call is Drupal\myentity\class\MyEntity it will autoload sites\all\modules\Drupal\myentity\class\MyEntity.php I believe you can tell it to look in a different folder, but the same sort of "structure" will apply.
    – Steven
    Mar 31, 2014 at 17:59
  • Yes, it uses PSR-0 (and optionally PSR-4).
    – donquixote
    Mar 31, 2014 at 18:03
  • drupal.org/node/2229605 is this you?
    – donquixote
    Mar 31, 2014 at 18:05
  • This code features so many different mistakes that it would be a good candidate for a "troubleshooting" link for the xautoload module :)
    – donquixote
    Mar 31, 2014 at 18:36
  • hehe glad I was somehow "useful" haha
    – yeahman
    Mar 31, 2014 at 18:39

1 Answer 1

2

Bugs bugs bugs.

    'entity class' => 'Drupal/myentity/class/MyEntity',
    'controller class' => 'Drupal/myentity/controller/MyEntityController',

This should be

    'entity class' => 'Drupal\myentity\class\MyEntity',
    'controller class' => 'Drupal\myentity\controller\MyEntityController',

And:

namespace Drupal\myentity\class\MyEntity;

This should be

namespace Drupal\myentity\class;

(and btw, is "class" really a valid namespace part? Won't PHP complain?)

And:

sites\all\modules\myentity\lib\Drupal\controller\MyEntityController.php

This should be:

sites\all\modules\myentity\lib\Drupal\myentity\controller\MyEntityController.php

EDIT: One more:

class MyEntity extends Entity {

should be

class MyEntity extends \Entity {

Because class Entity is in root namespace.

15
  • 1
    I thought that PSR stuff was messed up, and yes class or Class will cause PHP to throw up.
    – Steven
    Mar 31, 2014 at 18:04
  • So, does this work for you now?
    – donquixote
    Mar 31, 2014 at 18:10
  • thx for the reply.. i am always confused with the slashes lol (same for directory slashes.. I sometimes mess them up...) seems to work except i get that error now: Fatal error: Class 'Drupal\myentity\controller\EntityAPIController' not found in \sites\all\modules\al\myentity\lib\Drupal\myentity\controller\MyEntityController.php on line 8 my controller class is extending EntityAPIController: class MyEntityController extends EntityAPIController
    – yeahman
    Mar 31, 2014 at 18:14
  • 1
    Replace "extends EntityAPIController" with "extends \EntityAPIController".
    – donquixote
    Mar 31, 2014 at 18:30
  • 1
    "why do I need to add this slash" - Because if your code is in a namespace, then every class name will be assumed to be relative to this namespace. So if you write "extends Entity" it will assume this means "extends \Drupal\myentity\..\Entity" instead of just "extends \Entity". The preceding backslash means this is an absolute namespace.
    – donquixote
    Mar 31, 2014 at 18:34

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.