Hi @zengenuity,
First of all, thank you for this module! I recently coded a custom wrapper module witch is extremely similar, but less functional :-)
Here is my issue:
Given a 'job_offer' Entity, with a single bundle 'job_offer' and no fields, when creating a JobOfferJobOfferWrapper
instance:
$job_offer = JobOfferJobOfferWrapper::create([
'title' => 'Job: test',
]);
The following errors are raised:
Missing argument 2 for WdEntityWrapper::__construct(), called in
/home/vagrant/www/html/sites/all/modules/custom/bae_wrappers/includes/job_offer/JobOfferJobOfferWrapper.php on line 21 and defined in WdEntityWrapper.php:35
Illegal offset type in isset or empty entity.module:1404
strpos() expects parameter 1 to be string, object given entity.property.inc:351
Here is the begining of the generated JobOfferJobOfferWrapper
class:
class JobOfferJobOfferWrapper extends WdEntityWrapper {
private static $bundle = 'job_offer';
/**
* Create a new job_offer job_offer.
*
* @param array $values
* @param string $language
* @return JobOfferJobOfferWrapper
*/
public static function create($values = array(), $language = LANGUAGE_NONE) {
$values += array('bundle' => self::$bundle);
$entity_wrapper = parent::create($values, $language);
return new JobOfferJobOfferWrapper($entity_wrapper->value());
}
..
}
JobOfferJobOfferWrapper
does not have a __construct()
method, so WdEntityWrapper::__construct()
is called.
Here is the WdEntityWrapper::__construct()
signature:
/**
* Wrap an entity.
*
* @param $entity_type
* Entity type
*
* @param stdClass|int $entity
* Entity to wrap. Will load entity if ID is passed.
*/
public function __construct($entity_type, $entity) {}
So this contructor args are not respected, hence the errors...
Am I missing something or JobOfferJobOfferWrapper::create()
should return something like new JobOfferJobOfferWrapper($entity_wrapper->entity_type, $entity_wrapper->value());
?
Thanks,
Manu
Comments
Comment #1
manu manucorrected a typo.
Comment #2
zengenuity CreditAttribution: zengenuity commentedGlad you like the module!
WdEntityWrapper::create() requires that one of the parameters of the values array be entity_type. Change the first line in your create() method to this:
Comment #3
manu manuThanks for your return @zengenuity,
Actually this is a separate issue I was about to create :-) , and (at least the current version)
EntityMetadataWrapper
requires a key named 'type' instead of 'bundle'.But the instance creation is still a problem, because of
WdEntityWrapper::__construct()
being passed a wrong argument...Don't you experience the same problems when using the
create()
method?Thanks again
Comment #4
manu manuPlease forget my comment about the 'bundle' key, I have to check this.
Comment #5
zengenuity CreditAttribution: zengenuity commentedIn addition to the change in #2, this line should be changed
to this
Comment #6
zengenuity CreditAttribution: zengenuity commentedIdeally, you should add a constructor to your class so you don't have to pass in the entity type. It's redundant. If you look at the base entity classes in the includes folder, you can see some examples.
I haven't been using the wrappers without a base entity class for each type, so your scenario is different than I've been testing with. I think I can probably have the drush command generate a constructor automatically when there is no base entity class. Though, in general, it's most useful to write a base entity class first to expose entity properties. Then generate classes for each bundle. It looks like your entity type may only have one bundle, though, so I can see where it might not be necessary in your case.
Comment #7
manu manuOkay, so I should add a base class, and implement
hook_wrappers_delight_base_classes
?Comment #8
zengenuity CreditAttribution: zengenuity commentedYes. I need to write some documentation for this, but you can model your function on
wrappers_delight_wrappers_delight_base_classes()
.Make sure to add your base class to your module's .info file.
You can probably start with the generated class you have, take out any field-specific methods and add a constructor.
Comment #9
manu manuThanks,
It seems that
wrappers_delight_get_base_classes()
is not called anywhere... right?Comment #10
zengenuity CreditAttribution: zengenuity commentedSorry, you're right. It looks like I didn't finish that feature yet. It a call to it needs to be added to wrappers_delight_get_parent_class().
Comment #11
manu manuOk, changing the issue status as it's not really a bug.
Right now I added a base class for my entities and changed the parent class by hand, but the project I am working on is in bootstrap phase and I suspect that more custom entities are coming. So I may have the possibility to contribute a patch.
Comment #13
zengenuity CreditAttribution: zengenuity commentedI've fixed the hook for declaring base wrapper classes for new entity types. Here is the example doc for the hook. Pretty simple. You just need to make sure you're including your class file in the .info file. Wrappers Delight doesn't do any autoloading.
Comment #15
manu manuThank you zengenuity.