0

I have a custom entity with a constructor that calls a custom method (generateCharacteristics()) to generate random values for certain fields of my entity :

    public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) {

    parent::__construct($values, $entity_type, $bundle = FALSE, $translations = array());

    $values += $this->generateCharacteristics();

    foreach ($values as $key => $value)
    {

        $method = 'set'.ucfirst($key);

        // For two-words attributes
        if( strstr($method, '_') ) {
            $method = explode('_', $method);
            $method[1] = ucfirst($method[1]);
            $method = implode('', $method);
        }

        if (method_exists($this, $method))
        {
          $this->$method($value);
        }
    }


}

When I log what occurs in this __construct method when I create a new entity, all works fine, attributes are well setted, but in my database I have NULL or default values (the ones I declared in baseFieldDefinitions()). That's a shame because I think that baseFieldDefinitions() is put in cache (not good for random values) and that my method using constructor is cleaner.

Maybe my error is to think that $values array is the array we send during the call of Entity::create() method because this $values array seems always empty before I add my own array to $values... Nevertheless I set my attributes correctly in the constructor. The use of a postCreate() method should be more appropriate ?

thanks !

  • api.drupal.org/api/drupal/… – user21641 Oct 18 '17 at 16:30
  • @IvanJaros, the hook is more for existing entities, when you build a custom entity you want to put the code in the entity class. – 4k4 Oct 18 '17 at 19:00
  • i have no idea what is the point of that code, only that using constructor is bad. – user21641 Oct 18 '17 at 20:43
  • There's a lot happening in the parent constructor (You should take a look). Try running parent::_construct after $values += $this->generateCharacteristics();. I don't think you even need to call those setter methods. It should get set in the parent constructor. – Beebee Oct 24 '17 at 15:18
  • You do save the entity right? Just creating it only instantiates the entity, but it is gone once the request ends. You have to call Entity::save() separately. Since you say it works when you inspect it, I figured this might be the cause. – Neograph734 Oct 25 '17 at 18:44
2

Entity::create gets the storage for the entity type and calls EntityStorage::create. What you are looking for is probably the method Entity::preCreate, which is the first entity event invoked by EntityStorage::create.

Place it as public static function in the custom entity class to change the values before the entity is created:

class CustomEntity extends ContentEntityBase {

   /**
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    // modify $values
  }

}
| improve this answer | |

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.