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

manu manu’s picture

Issue summary: View changes

corrected a typo.

zengenuity’s picture

Glad 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:

<?php
$values += array('entity_type' => 'job_offer', 'bundle' => self::$bundle);
?>
manu manu’s picture

Thanks 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

manu manu’s picture

Please forget my comment about the 'bundle' key, I have to check this.

zengenuity’s picture

In addition to the change in #2, this line should be changed

<?php
return new JobOfferJobOfferWrapper($entity_wrapper->value());
?>

to this

<?php
return new JobOfferJobOfferWrapper('job_offer', $entity_wrapper->value());
?>
zengenuity’s picture

Ideally, 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.

manu manu’s picture

Okay, so I should add a base class, and implement hook_wrappers_delight_base_classes?

zengenuity’s picture

Yes. 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.

manu manu’s picture

Thanks,

It seems that wrappers_delight_get_base_classes() is not called anywhere... right?

zengenuity’s picture

Sorry, 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().

manu manu’s picture

Title: Wrapper instance creation problem » Call wrappers_delight_get_base_classes() in wrappers_delight_get_parent_class()
Category: Bug report » Feature request

Ok, 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.

  • zengenuity committed ad6d4ba on 7.x-1.x
    #2349689 - Fix the hook to allow modules to declare base classes and...
zengenuity’s picture

Status: Active » Fixed

I'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.

<?php
function hook_wrappers_delight_base_classes() {
  return array(
    'example_entity' => 'WdExampleEntityWrapper',
  );
}
?>

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

manu manu’s picture

Thank you zengenuity.