- <?php
-
-
- * EntityExampleBasic extends Entity.
- *
- * A subclass of Entity is a requirement for creating a custom entity. It is
- * also a minimum requirement to define the methods included here, even as
- * empty functions.
- */
- class EntityExampleBasic extends Entity {
-
-
- * The basic ID.
- *
- * @var integer
- */
- public $basic_id;
-
-
- * Item description.
- */
- public $item_description;
-
-
- * The bundle type.
- *
- * @var string
- */
- public $bundle_type;
-
-
- * Implements EntityInterface::id().
- */
- public function id() {
- return isset($this->basic_id) ? $this->basic_id : NULL;
- }
-
-
- * Implements EntityInterface::entityType().
- */
- public function entityType() {
- return 'entity_example_basic';
- }
-
-
- * Implements EntityInterface::bundle().
- */
- public function bundle() {
- return $this->bundle_type;
- }
-
-
- * Implements EntityInterface::label().
- */
- public function label() {
- return $this->item_description;
- }
-
-
- * Implements EntityInterface::uri().
- */
- public function uri() {
- return array(
- 'path' => 'examples/entity_example/basic/' . $this->basic_id,
- 'options' => array(),
- );
- }
- }
-
- * EntityExampleBasicControllerInterface definition.
- *
- * We create an interface here because anyone could come along and
- * use hook_entity_info_alter() to change our controller class.
- * We want to let them know what methods our class needs in order
- * to function with the rest of the module, so here's a handy list.
- *
- * @see hook_entity_info_alter()
- */
- interface EntityExampleBasicControllerInterface
- extends EntityControllerInterface {
-
-
- * Create an entity.
- */
- public function create();
-
-
- * Save an entity.
- *
- * @param object $entity
- * The entity to save.
- */
- public function save($entity);
-
-
- * Delete an entity.
- *
- * @param object $entity
- * The entity to delete.
- */
- public function delete($entity);
-
- }
-
- * EntityExampleBasicController extends BackdropDefaultEntityController.
- *
- * Our subclass of BackdropDefaultEntityController lets us add a few
- * important create, update, and delete methods.
- */
- class EntityExampleBasicController
- extends DefaultEntityController
- implements EntityExampleBasicControllerInterface {
-
-
- * Create and return a new entity_example_basic entity.
- */
- public function create() {
-
-
-
-
-
- $entity = new EntityExampleBasic();
- $entity->type = 'entity_example_basic';
- $entity->basic_id = 0;
- $entity->bundle_type = 'first_example_bundle';
- $entity->item_description = '';
- return $entity;
- }
-
-
- * Saves the custom fields using backdrop_write_record().
- */
- public function save($entity) {
-
-
- if (empty($entity->basic_id)) {
- $entity->created = time();
- }
-
- module_invoke_all('entity_presave', $entity, 'entity_example_basic');
-
-
-
- $primary_keys = $entity->basic_id ? 'basic_id' : array();
-
- backdrop_write_record('entity_example_basic', $entity, $primary_keys);
-
-
-
-
- $invocation = 'entity_insert';
-
-
-
-
- if (empty($primary_keys)) {
- field_attach_insert('entity_example_basic', $entity);
- }
- else {
- field_attach_update('entity_example_basic', $entity);
- $invocation = 'entity_update';
- }
-
- module_invoke_all($invocation, $entity, 'entity_example_basic');
- return $entity;
- }
-
-
- * Delete a single entity.
- *
- * Really a convenience function for deleteMultiple().
- */
- public function delete($entity) {
- $this->deleteMultiple(array($entity));
- }
-
-
- * Delete one or more entity_example_basic entities.
- *
- * Deletion is unfortunately not supported in the base
- * BackdropDefaultEntityController class.
- *
- * @param array $entities
- * An array of entity IDs or a single numeric ID.
- */
- public function deleteMultiple($entities) {
- $basic_ids = array();
- if (!empty($entities)) {
- $transaction = db_transaction();
- try {
- foreach ($entities as $entity) {
-
- module_invoke_all('entity_delete', $entity, 'entity_example_basic');
- field_attach_delete('entity_example_basic', $entity);
- $basic_ids[] = $entity->basic_id;
- }
- db_delete('entity_example_basic')
- ->condition('basic_id', $basic_ids, 'IN')
- ->execute();
- }
- catch (Exception $e) {
- $transaction->rollback();
- watchdog_exception('entity_example', $e);
- throw $e;
- }
- }
- }
- }
-