Presented by Tom Friedhof
http://youtube.com/activelamp
Users, Terms, Panels, Everything!
https://www.drupal.org/project/entity
Real objects... no more stdClass()
/**
* @file
* Tournament match class.
*/
class TourneyMatchEntity extends TourneyEntity {
...
/**
* Get the winner of the match
*/
public function getWinner() {
if (is_null($this->matchWinner)) {
$this->matchWinner = $this->determineWinner();
}
return $this->matchWinner;
}
...
}
/**
* Get the winner of the match
*/
$match = tourney_match_load(1);
/**
* Call method directly on object.
*/
$winner = $match->getWinner();
class TourneyEntity extends Entity {
...
public function save() {
if ((isset($this->is_new) && $this->is_new) || $this->created == 0) {
$this->created = time();
}
$this->changed = time();
parent::save();
}
/**
* Return the uri to the entity object
*/
public function getUri() {
return TourneyEntity::path($this->id, $this->entityType);
}
...
}
Super powerful!
Next session in Room G7
/**
* Implements hook_entity_info().
*/
function it_service_entity_info() {
$info = array(
'it_service' => array(
'label' => t('IT Service'),
'plural label' => t('IT Services'),
'description' => t('A single service provided by IT Services.'),
'entity class' => 'Drupal\it_service\Entity\ItServiceProxy',
'controller class' => 'EntityAPIController',
'base table' => 'it_service',
'fieldable' => TRUE,
...
)
);
return $info;
}
namespace Drupal\it_service\Entity;
...
class ItServiceProxy extends ItServiceBase {
protected $entityClass;
protected $entityType = 'it_service';
/**
* ItServiceProxy constructor.
*/
public function __construct(array $values, $entityType) {
$values['type'] = !empty($this->type) ? $this->type : $values['type'];
$this->type = $values['type'];
if (is_null($this->entityClass)) {
$this->makeEntity($values['type']);
}
}
private function makeEntity($bundle) {
$className = 'Drupal\it_service\Entity\\' . CaseConverter::toCamelCase($bundle);
if (class_exists($className)) {
$this->entityClass = new $className(['type' => $bundle], $this->entityType);
}
else {
throw new \Exception(t('A class does not exist for @bundleName bundle.', ['@bundleName' => $className]));
}
}
function __call($name, $args) {
if (!method_exists($this->entityClass, $name)) return FALSE;
$method = new \ReflectionMethod($this->entityClass, $name);
return $method->invoke($this->entityClass, $args);
}
}
http://youtube.com/activelamp
Send me an email:
tom@activelamp.com