class Box in Boxes 7.2
The Box entity class
Hierarchy
- class \Entity implements EntityInterface
- class \Box
Expanded class hierarchy of Box
1 string reference to 'Box'
- boxes_entity_info in ./
boxes.module - Implements hook_entity_info().
File
- includes/
boxes.core.inc, line 139 - Box classes and plugin interface
View source
class Box extends Entity {
public $label;
public $description;
public $title;
public $type;
public $data;
public $delta;
public $view_mode;
protected $plugin;
/**
* Get Plugin info
*
* @param $key
* The plugin key to get the info for.
*/
public function getInfo($key = NULL) {
if ($this->plugin instanceof BoxTypePluginInterface) {
return $this->plugin
->getInfo($key);
}
return NULL;
}
public function defaultLabel() {
return $this->label;
}
public function __construct(array $values = array()) {
// If this is new then set the type first.
if (isset($values['is_new'])) {
$this->type = isset($values['type']) ? $values['type'] : '';
}
parent::__construct($values, 'box');
}
protected function setUp() {
parent::setUp();
if (!empty($this->type)) {
$plugin = boxes_load_plugin_class($this->type);
$this
->loadUp($plugin);
}
}
/**
* This is a work around for version of PDO that call __construct() before
* it loads up the object with values from the DB.
*/
public function loadUp($plugin) {
if (empty($this->plugin)) {
// If the plugin has been disabled, the $plugin will be a boolean.
// just load the base plugin so something will render.
if (!$plugin instanceof BoxTypePluginInterface) {
$plugin = boxes_load_plugin_class('box_default');
}
$this
->setPlugin($plugin);
$this
->setFields();
}
}
/**
* Load and set the plugin info.
* This can be called externally via loadUP()
*/
protected function setPlugin($plugin) {
$this->plugin = $plugin;
}
/**
* Set the fields from the defaults and plugin
* This can be called externally via loadUP()
*/
protected function setFields() {
// NOTE: When setFields is called externally $this->data is already unserialized.
if (!empty($this->plugin) && !empty($this->type)) {
$values = is_array($this->data) ? $this->data : unserialize($this->data);
foreach ($this->plugin
->values() as $field => $default) {
$this->{$field} = isset($values[$field]) ? $values[$field] : $default;
}
}
}
/**
* Override this in order to implement a custom default URI and specify
* 'entity_class_uri' as 'uri callback' hook_entity_info().
*/
protected function defaultUri() {
return array(
'path' => 'block/' . $this
->identifier(),
);
}
/**
* Get the plugin form
*/
public function getForm($form, &$form_state) {
return $this->plugin
->form($this, $form, $form_state);
}
/**
* Validate the plugin form
*/
public function validate($values, &$form_state) {
$this->plugin
->validate($values, $form_state);
}
/**
* URL string
*/
public function url() {
$uri = $this
->defaultUri();
return $uri['path'];
}
/**
* Build the URL string
*/
protected function buildURL($type) {
$url = $this
->url();
return $url . '/' . $type;
}
/**
* View URL
*/
public function viewURL() {
return $this
->buildURL('view');
}
/**
* View URL
*/
public function editURL() {
return $this
->buildURL('edit');
}
/**
* View URL
*/
public function deleteURL() {
return $this
->buildURL('delete');
}
/**
* Type name
*/
public function typeName() {
return $this
->getInfo('label');
}
/**
* Set the values from a plugin
*/
public function setValues($values) {
$this->data = array();
foreach ($this->plugin
->values() as $field => $value) {
$this->data[$field] = $values[$field];
}
}
/**
* Generate an array for rendering the entity.
*
* @see entity_view()
*/
public function view($view_mode = 'default', $langcode = NULL, $page = NULL) {
$content = parent::view($view_mode, $langcode);
if (empty($this->plugin)) {
return $content;
}
return $this->plugin
->view($this, $content, $view_mode, $langcode);
}
/**
* Override the save to add clearing of caches
*/
public function save() {
// Set the delta if it's not set already
if (empty($this->delta)) {
$max_length = 32;
// Base it on the label and make sure it isn't too long for the database.
$this->delta = drupal_clean_css_identifier(strtolower($this->label));
$this->delta = substr($this->delta, 0, $max_length);
// Check if delta is unique.
if (boxes_load_delta($this->delta)) {
$i = 0;
$separator = '-';
$original_delta = $this->delta;
do {
$unique_suffix = $separator . $i++;
$this->delta = substr($original_delta, 0, $max_length - drupal_strlen($unique_suffix)) . $unique_suffix;
} while (boxes_load_delta($this->delta));
}
}
$this->plugin
->submit($this);
$return = parent::save();
block_flush_caches();
cache_clear_all('boxes:' . $this->delta . ':', 'cache_block', TRUE);
return $return;
}
/**
* Override the delete to remove the fields and blocks
*/
public function delete() {
if ($this
->internalIdentifier()) {
// Delete the field values
field_attach_delete('box', $this);
// Delete any blocks
// @see block_custom_block_delete_submit()
db_delete('block')
->condition('module', 'boxes')
->condition('delta', $this
->identifier())
->execute();
db_delete('block_role')
->condition('module', 'boxes')
->condition('delta', $this
->identifier())
->execute();
// @see node_form_block_custom_block_delete_submit()
db_delete('block_node_type')
->condition('module', 'boxes')
->condition('delta', $this
->identifier())
->execute();
entity_get_controller($this->entityType)
->delete(array(
$this
->internalIdentifier(),
));
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Box:: |
public | property | ||
Box:: |
public | property | ||
Box:: |
public | property | ||
Box:: |
public | property | ||
Box:: |
protected | property | ||
Box:: |
public | property | ||
Box:: |
public | property | ||
Box:: |
public | property | ||
Box:: |
protected | function | Build the URL string | |
Box:: |
public | function |
Defines the entity label if the 'entity_class_label' callback is used. Overrides Entity:: |
|
Box:: |
protected | function |
Override this in order to implement a custom default URI and specify
'entity_class_uri' as 'uri callback' hook_entity_info(). Overrides Entity:: |
|
Box:: |
public | function |
Override the delete to remove the fields and blocks Overrides Entity:: |
|
Box:: |
public | function | View URL | |
Box:: |
public | function | View URL | |
Box:: |
public | function | Get the plugin form | |
Box:: |
public | function | Get Plugin info | |
Box:: |
public | function | This is a work around for version of PDO that call __construct() before it loads up the object with values from the DB. | |
Box:: |
public | function |
Override the save to add clearing of caches Overrides Entity:: |
|
Box:: |
protected | function | Set the fields from the defaults and plugin This can be called externally via loadUP() | |
Box:: |
protected | function | Load and set the plugin info. This can be called externally via loadUP() | |
Box:: |
protected | function |
Set up the object instance on construction or unserializiation. Overrides Entity:: |
|
Box:: |
public | function | Set the values from a plugin | |
Box:: |
public | function | Type name | |
Box:: |
public | function | URL string | |
Box:: |
public | function | Validate the plugin form | |
Box:: |
public | function |
Generate an array for rendering the entity. Overrides Entity:: |
|
Box:: |
public | function | View URL | |
Box:: |
public | function |
Overrides Entity:: |
|
Entity:: |
protected | property | 1 | |
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
public | function |
Builds a structured array representing the entity's content. Overrides EntityInterface:: |
1 |
Entity:: |
public | function |
Returns the bundle of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the info of the type of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the type of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Exports the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Gets the raw, translated value of a property or field. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Checks if the entity has a certain exportable status. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the internal, numeric identifier. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Checks whether the entity is the default revision. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the label of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the uri of the entity just as entity_uri(). Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function | Magic method to only serialize what's necessary. | |
Entity:: |
public | function | Magic method to invoke setUp() on unserialization. |