View source
<?php
interface PanelizerEntityInterface {
public function init($plugin);
public function hook_menu(&$items);
public function hook_menu_alter(&$items);
public function hook_form_alter(&$form, &$form_state, $form_id);
public function hook_permission(&$items);
public function hook_admin_paths(&$items);
public function hook_views_data_alter(&$data);
public function hook_entity_load(&$entities);
public function hook_entity_insert($entity);
public function hook_entity_update($entity);
public function hook_entity_delete($entity);
public function hook_field_attach_delete_revision($entity);
public function settings_form(&$form, &$form_state);
public function settings_form_validate(&$form, &$form_state);
public function settings_form_submit(&$form, &$form_state);
public function get_default_panelizer_object($bundle, $name);
public function access_default_panelizer_object($panelizer);
public function is_panelized($bundle);
public function has_default_panel($bundle);
public function has_panel_choice($bundle);
function get_default_display();
function get_panelizer_object($key);
function render_entity($entity, $args = array());
public function get_contexts($panelizer, $entity = NULL);
public function get_base_contexts($entity = NULL);
public function entity_access($op, $entity);
public function entity_save($entity);
public function entity_allows_revisions($entity);
public function entity_identifier($entity);
public function entity_bundle_label();
}
abstract class PanelizerEntityDefault implements PanelizerEntityInterface {
public $entity_admin_root = NULL;
public $supports_revisions = FALSE;
public $views_table = '';
public $plugin = NULL;
public $entity_type = '';
function init($plugin) {
$this->plugin = $plugin;
$this->entity_type = $plugin['name'];
}
public function hook_permission(&$items) {
$entity_info = entity_get_info($this->entity_type);
foreach ($this->plugin['bundles'] as $bundle => $settings) {
$items["administer panelizer {$this->entity_type} {$bundle} defaults"] = array(
'title' => t('%entity_name %bundle_name: Administer Panelizer default panels, allowed content and settings.', array(
'%entity_name' => $entity_info['label'],
'%bundle_name' => $entity_info['bundles'][$bundle]['label'],
)),
'description' => t('Users with this permission can fully administer panelizer for this entity bundle.'),
);
if (empty($settings['status'])) {
continue;
}
$items["administer panelizer {$this->entity_type} {$bundle} content"] = array(
'title' => t('%entity_name %bundle_name: Administer Panelizer content', array(
'%entity_name' => $entity_info['label'],
'%bundle_name' => $entity_info['bundles'][$bundle]['label'],
)),
);
$items["administer panelizer {$this->entity_type} {$bundle} context"] = array(
'title' => t('%entity_name %bundle_name: Administer Panelizer context', array(
'%entity_name' => $entity_info['label'],
'%bundle_name' => $entity_info['bundles'][$bundle]['label'],
)),
);
$items["administer panelizer {$this->entity_type} {$bundle} layout"] = array(
'title' => t('%entity_name %bundle_name: Administer Panelizer layout', array(
'%entity_name' => $entity_info['label'],
'%bundle_name' => $entity_info['bundles'][$bundle]['label'],
)),
);
$items["administer panelizer {$this->entity_type} {$bundle} settings"] = array(
'title' => t('%entity_name %bundle_name: Administer Panelizer settings', array(
'%entity_name' => $entity_info['label'],
'%bundle_name' => $entity_info['bundles'][$bundle]['label'],
)),
);
if (!empty($settings['choice'])) {
$items["administer panelizer {$this->entity_type} {$bundle} choice"] = array(
'title' => t('%entity_name %bundle_name: Choose panels', array(
'%entity_name' => $entity_info['label'],
'%bundle_name' => $entity_info['bundles'][$bundle]['label'],
)),
'description' => t('Allows the user to choose which default panel the entity uses.'),
);
}
}
}
public function hook_menu(&$items) {
if (!empty($this->plugin['entity path'])) {
$bits = explode('/', $this->plugin['entity path']);
foreach ($bits as $count => $bit) {
if (strpos($bit, '%') === 0) {
$position = $count;
break;
}
}
if (!isset($position)) {
return;
}
$total = count($bits);
$base = array(
'access callback' => 'panelizer_entity_plugin_callback_switcher',
'access arguments' => array(
$this->entity_type,
'access',
'admin',
$position,
'content',
),
'type' => MENU_LOCAL_TASK,
);
$items[$this->plugin['entity path'] . '/panelizer'] = array(
'title' => 'Panelizer',
'page callback' => 'panelizer_entity_plugin_switcher_page',
'page arguments' => array(
$this->entity_type,
'content',
$position,
),
'weight' => 11,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
) + $base;
$items[$this->plugin['entity path'] . '/panelizer/content'] = array(
'title' => 'Content',
'page callback' => 'panelizer_entity_plugin_switcher_page',
'page arguments' => array(
$this->entity_type,
'content',
$position,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 11,
) + $base;
$items[$this->plugin['entity path'] . '/panelizer/layout'] = array(
'title' => 'Layout',
'page callback' => 'panelizer_entity_plugin_switcher_page',
'page arguments' => array(
$this->entity_type,
'layout',
$position,
),
'access arguments' => array(
$this->entity_type,
'access',
'admin',
$position,
'layout',
),
'weight' => 12,
) + $base;
$items[$this->plugin['entity path'] . '/panelizer/context'] = array(
'title' => 'Context',
'page callback' => 'panelizer_entity_plugin_switcher_page',
'page arguments' => array(
$this->entity_type,
'context',
$position,
),
'access arguments' => array(
$this->entity_type,
'access',
'admin',
$position,
'context',
),
'weight' => 13,
) + $base;
$items[$this->plugin['entity path'] . '/panelizer/settings'] = array(
'title' => 'Settings',
'page callback' => 'panelizer_entity_plugin_switcher_page',
'page arguments' => array(
$this->entity_type,
'settings',
$position,
),
'access arguments' => array(
$this->entity_type,
'access',
'admin',
$position,
'settings',
),
'weight' => 14,
) + $base;
}
if (!empty($this->entity_admin_root)) {
$this
->add_admin_links($this->entity_admin_root, $this->entity_admin_bundle, $items);
}
}
public function add_admin_links($root, $bundle, &$items) {
$parts = explode('/', $root);
$base_count = count($parts);
$settings_base = array(
'access callback' => 'panelizer_is_panelized',
'access arguments' => array(
$this->entity_type,
$bundle,
),
'file' => 'includes/admin.inc',
);
$items[$root . '/panelizer'] = array(
'title' => 'Panelizer',
'page callback' => 'panelizer_allowed_content_page',
'page arguments' => array(
$this->entity_type,
$bundle,
),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
) + $settings_base;
$items[$root . '/panelizer/allowed'] = array(
'title' => 'Available content',
'page callback' => 'panelizer_allowed_content_page',
'page arguments' => array(
$this->entity_type,
$bundle,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
) + $settings_base;
$tabs_base = array(
'access callback' => 'panelizer_has_no_choice_callback',
'access arguments' => array(
$this->entity_type,
$bundle,
),
'page arguments' => array(
$this->entity_type,
$bundle,
'default',
),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/admin.inc',
);
$items[$root . '/panelizer/settings'] = array(
'title' => 'Settings',
'page callback' => 'panelizer_default_settings_page',
'weight' => -5,
) + $tabs_base;
$items[$root . '/panelizer/context'] = array(
'title' => 'Contexts',
'page callback' => 'panelizer_default_context_page',
'weight' => -4,
) + $tabs_base;
$items[$root . '/panelizer/layout'] = array(
'title' => 'Layout',
'page callback' => 'panelizer_default_layout_page',
'weight' => -3,
) + $tabs_base;
$items[$root . '/panelizer/content'] = array(
'title' => 'Content',
'page callback' => 'panelizer_default_content_page',
'weight' => -2,
) + $tabs_base;
$subtabs_base = array(
'access callback' => 'panelizer_administer_panelizer_default',
'access arguments' => array(
$this->entity_type,
$bundle,
$base_count + 2,
),
'page arguments' => array(
$this->entity_type,
$bundle,
$base_count + 2,
),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/admin.inc',
);
$items[$root . '/panelizer/list/%'] = array(
'title' => 'Settings',
'page callback' => 'panelizer_default_settings_page',
'title callback' => 'panelizer_default_name_title_callback',
'type' => MENU_CALLBACK,
) + $subtabs_base;
$items[$root . '/panelizer/list/%/settings'] = array(
'title' => 'Settings',
'page callback' => 'panelizer_default_settings_page',
'weight' => -5,
) + $subtabs_base;
$items[$root . '/panelizer/list/%/context'] = array(
'title' => 'Contexts',
'page callback' => 'panelizer_default_context_page',
'weight' => -4,
) + $subtabs_base;
$items[$root . '/panelizer/list/%/layout'] = array(
'title' => 'Layout',
'page callback' => 'panelizer_default_layout_page',
'weight' => -3,
) + $subtabs_base;
$items[$root . '/panelizer/list/%/content'] = array(
'title' => 'Content',
'page callback' => 'panelizer_default_content_page',
'weight' => -2,
) + $subtabs_base;
$items[$root . '/panelizer/list/%/access'] = array(
'title' => 'Access',
'page callback' => 'panelizer_default_access_page',
'weight' => -2,
) + $subtabs_base;
$plugin = ctools_get_export_ui('panelizer_defaults');
$ui_items = $plugin['menu']['items'];
$ui_items['list']['type'] = MENU_LOCAL_TASK;
$ui_items['list']['weight'] = -6;
$ui_items['list']['title'] = 'List';
$ui_items['add']['path'] = 'list/add';
$ui_items['import']['path'] = 'list/import';
unset($ui_items['edit']);
unset($ui_items['edit callback']);
unset($ui_items['list callback']);
unset($ui_items['context']);
unset($ui_items['content']);
unset($ui_items['layout']);
unset($ui_items['access']);
foreach ($ui_items as $key => $item) {
$ui_items[$key]['access callback'] = 'panelizer_has_choice_callback';
$ui_items[$key]['access arguments'] = array(
$this->entity_type,
$bundle,
'',
);
$ui_items[$key]['page callback'] = 'panelizer_export_ui_switcher_page';
array_unshift($ui_items[$key]['page arguments'], $bundle);
array_unshift($ui_items[$key]['page arguments'], $this->entity_type);
}
foreach ($ui_items as $item) {
$item += array(
'file' => 'export-ui.inc',
'file path' => drupal_get_path('module', 'ctools') . '/includes',
);
$path = !empty($item['path']) ? $root . '/panelizer/' . $item['path'] : $root . '/panelizer';
unset($item['path']);
$items[$path] = $item;
}
}
public function add_bundle_setting_form(&$form, &$form_state, $bundle, $type_location) {
$settings = !empty($this->plugin['bundles'][$bundle]) ? $this->plugin['bundles'][$bundle] : array(
'status' => FALSE,
'default' => FALSE,
'choice' => FALSE,
);
$form['panelizer'] = array(
'#type' => 'fieldset',
'#title' => t('Panelizer'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'panelizer-node-type-settings-form',
),
),
'#bundle' => $bundle,
'#location' => $type_location,
'#tree' => TRUE,
'#access' => panelizer_administer_entity_bundle($this, $bundle),
);
$form['panelizer']['status'] = array(
'#title' => t('Panelize'),
'#type' => 'checkbox',
'#default_value' => !empty($settings['status']),
'#id' => 'panelizer-status',
);
$form['panelizer']['default'] = array(
'#title' => t('Provide default panel'),
'#type' => 'checkbox',
'#default_value' => !empty($settings['default']),
'#states' => array(
'visible' => array(
'#panelizer-status' => array(
'checked' => TRUE,
),
),
),
'#description' => t('If checked, a default panel will be utilized for all existing and new entities.'),
);
$form['panelizer']['choice'] = array(
'#title' => t('Allow panel choice'),
'#type' => 'checkbox',
'#default_value' => !empty($settings['choice']),
'#states' => array(
'visible' => array(
'#panelizer-status' => array(
'checked' => TRUE,
),
),
),
'#description' => t('If checked multiple panels can be created and each entity will get a selector to choose which panel to use.'),
);
array_unshift($form['#submit'], 'panelizer_entity_default_bundle_form_submit');
$form_state['panelizer_entity_handler'] = $this;
}
public function add_bundle_setting_form_submit($form, &$form_state, $bundle, $type_location) {
if ($type_location) {
$new_bundle = drupal_array_get_nested_value($form_state['values'], $type_location);
}
else {
$new_bundle = $bundle;
}
if ($bundle && $new_bundle != $bundle) {
variable_del('panelizer_defaults_' . $this->entity_type . '_' . $bundle);
$allowed_layouts = variable_get('panelizer_' . $this->entity_type . ':' . $bundle . '_allowed_layouts', NULL);
if ($allowed_layouts) {
variable_del('panelizer_' . $this->entity_type . ':' . $bundle . '_allowed_layouts');
variable_set('panelizer_' . $this->entity_type . ':' . $new_bundle . '_allowed_layouts', $allowed_layouts);
}
$default = variable_get('panelizer_' . $this->entity_type . ':' . $bundle . '_default', NULL);
if ($default) {
variable_del('panelizer_' . $this->entity_type . ':' . $bundle . '_default');
variable_set('panelizer_' . $this->entity_type . ':' . $new_bundle . '_default', $default);
}
$panelizer_defaults = $this
->get_default_panelizer_objects($bundle);
foreach ($panelizer_defaults as $panelizer) {
list($entity_type, $old_bundle, $name) = explode(':', $panelizer->name);
$panelizer->name = implode(':', array(
$entity_type,
$new_bundle,
$name,
));
$panelizer->panelizer_key = $new_bundle;
ctools_export_crud_save('panelizer_defaults', $panelizer);
}
}
variable_set('panelizer_defaults_' . $this->entity_type . '_' . $new_bundle, $form_state['values']['panelizer']);
unset($form_state['values']['panelizer']);
}
public function hook_admin_paths(&$items) {
if (!empty($this->plugin['entity path'])) {
$bits = explode('/', $this->plugin['entity path']);
foreach ($bits as $count => $bit) {
if (strpos($bit, '%') === 0) {
$bits[$count] = '*';
}
}
$path = implode('/', $bits);
$items[$path . '/panelizer*'] = TRUE;
}
}
public function hook_menu_alter(&$items) {
}
public function hook_form_alter(&$form, &$form_state, $form_id) {
}
public function hook_entity_load(&$entities) {
ctools_include('export');
$ids = array();
$vids = array();
$bundles = array();
foreach ($entities as $entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
if ($this
->is_panelized($bundle)) {
$ids[] = $entity_id;
if ($this->supports_revisions) {
$vids[] = $revision_id;
}
$bundles[$entity_id] = $bundle;
}
}
if (!$ids) {
return;
}
if ($this->supports_revisions) {
$panelizers = db_query("SELECT * FROM {panelizer_entity} WHERE entity_type = '{$this->entity_type}' AND entity_id IN (:ids) AND revision_id IN (:vids)", array(
':ids' => $ids,
':vids' => $vids,
))
->fetchAllAssoc('entity_id');
}
else {
$panelizers = db_query("SELECT * FROM {panelizer_entity} WHERE entity_type = '{$this->entity_type}' AND entity_id IN (:ids)", array(
':ids' => $ids,
))
->fetchAllAssoc('entity_id');
}
$defaults = array();
$dids = array();
foreach ($entities as $entity_id => $entity) {
if (!empty($entity->panelizer)) {
continue;
}
if (empty($bundles[$entity_id])) {
continue;
}
if (empty($panelizers[$entity_id])) {
if ($this
->has_default_panel($bundles[$entity_id])) {
$name = implode(':', array(
$this->entity_type,
$bundles[$entity_id],
'default',
));
$defaults[$name] = $name;
}
}
else {
$entity->panelizer = ctools_export_unpack_object('panelizer_entity', $panelizers[$entity_id]);
if (empty($entity->panelizer->did) && !empty($entity->panelizer->name)) {
$defaults[] = $entity->panelizer->name;
}
else {
$dids[$entity->panelizer->did] = $entity->panelizer->did;
}
}
}
if ($defaults) {
$panelizer_defaults = $this
->load_default_panelizer_objects($defaults);
}
if ($dids) {
$displays = panels_load_displays($dids);
}
foreach ($entities as $entity_id => $entity) {
if (empty($bundles[$entity_id])) {
continue;
}
if (empty($entity->panelizer)) {
$default_key = implode(':', array(
$this->entity_type,
$bundles[$entity_id],
'default',
));
if (!empty($panelizer_defaults[$default_key])) {
$entity->panelizer = clone $panelizer_defaults[$default_key];
$entity->panelizer->did = NULL;
}
}
else {
if (empty($entity->panelizer->display)) {
if (!empty($entity->panelizer->did)) {
if (empty($displays[$entity->panelizer->did])) {
$entity->panelizer->did = NULL;
$entity->panelizer->display = $this
->get_default_display();
}
else {
$entity->panelizer->display = $displays[$entity->panelizer->did];
}
}
else {
if (!empty($panelizer_defaults[$entity->panelizer->name])) {
$entity->panelizer->display = $panelizer_defaults[$entity->panelizer->name]->display;
}
}
}
}
}
}
public function hook_entity_insert($entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
if (!$this
->is_panelized($bundle)) {
return;
}
if (empty($entity->panelizer)) {
return;
}
if (empty($entity->panelizer->name) && !empty($entity->panelizer->display)) {
$panelizer = $this
->clone_panelizer($entity->panelizer, $entity);
panels_save_display($panelizer->display);
$panelizer->did = $panelizer->display->did;
return drupal_write_record('panelizer_entity', $panelizer);
}
else {
$entity->panelizer->did = NULL;
$entity->panelizer->entity_type = $this->entity_type;
$entity->panelizer->entity_id = $entity_id;
$entity->panelizer->revision_id = (int) $revision_id;
return drupal_write_record('panelizer_entity', $entity->panelizer);
}
}
public function hook_entity_update($entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
if (!$this
->is_panelized($bundle)) {
return;
}
if (empty($entity->panelizer)) {
return;
}
if ($this->supports_revisions) {
if (empty($entity->panelizer->revision_id) || $entity->panelizer->revision_id != $revision_id) {
$update = array();
}
else {
$update = array(
'entity_type',
'revision_id',
);
}
}
else {
if (empty($entity->panelizer->entity_id)) {
$update = array();
}
else {
$update = array(
'entity_type',
'entity_id',
);
}
}
if (!empty($entity->panelizer->display_is_modified)) {
if (!$update || empty($entity->panelizer->did)) {
$panelizer = $this
->clone_panelizer($entity->panelizer, $entity);
}
else {
$panelizer = $entity->panelizer;
}
panels_save_display($panelizer->display);
$panelizer->did = $panelizer->display->did;
$panelizer->name = NULL;
return drupal_write_record('panelizer_entity', $panelizer, $update);
}
else {
$entity->panelizer->entity_type = $this->entity_type;
$entity->panelizer->entity_id = $entity_id;
$entity->panelizer->revision_id = (int) $revision_id;
drupal_write_record('panelizer_entity', $entity->panelizer, $update);
}
}
public function hook_entity_delete($entity) {
$this
->delete_entity_panelizer($entity);
}
public function hook_field_attach_delete_revision($entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
$revisions = db_query("SELECT revision_id, did FROM {panelizer_entity} WHERE entity_type = '{$this->entity_type}' AND entity_id = :id", array(
':id' => $entity_id,
))
->fetchAllAssoc('revision_id');
$seen = array();
foreach ($revisions as $info) {
if ($info->revision_id != $revision_id) {
$seen[$info->did] = TRUE;
}
}
if (!empty($revisions[$revision_id]->did) && empty($seen[$revisions[$revision_id]->did])) {
panels_delete_display($revisions[$revision_id]->did);
}
db_delete('panelizer_entity')
->condition('entity_type', $this->entity_type)
->condition('entity_id', $entity_id)
->condition('revision_id', $revision_id)
->execute();
}
public function hook_field_attach_form($entity, &$form, &$form_state, $langcode) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
if (!$this
->has_panel_choice($bundle) || !empty($entity->panelizer->did)) {
return;
}
$form_state['panelizer has choice'] = TRUE;
$form['panelizer'] = array(
'#type' => 'fieldset',
'#access' => $this
->panelizer_access('choice', $entity),
'#title' => t('Panelizer'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'panelizer-entity-options',
),
),
'#attached' => array(
'js' => array(
ctools_attach_js('panelizer-vertical-tabs', 'panelizer'),
),
),
'#weight' => -10,
'#tree' => TRUE,
'#revision_id' => isset($entity->panelizer->revision_id) ? $entity->panelizer->revision_id : NULL,
'#entity_id' => isset($entity->panelizer->entity_id) ? $entity->panelizer->entity_id : NULL,
);
$panelizers = $this
->get_default_panelizer_objects($bundle);
$options = array();
foreach ($panelizers as $name => $panelizer) {
if (empty($panelizer->disabled)) {
$options[$name] = $panelizer->title ? $panelizer->title : t('Default');
}
}
if (!empty($entity->panelizer->name)) {
$name = $entity->panelizer->name;
}
else {
if ($this
->has_default_panel($bundle)) {
$name = implode(':', array(
$this->entity_type,
$bundle,
'default',
));
}
else {
$name = '';
}
}
if (!$this
->has_default_panel($bundle)) {
$options = array(
'' => t('-- No panel --'),
) + $options;
}
$form['panelizer']['name'] = array(
'#title' => t('Panel'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $name,
);
}
public function hook_field_attach_submit($entity, &$form, &$form_state) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
if (!empty($form_state['panelizer has choice']) && !empty($form_state['values']['panelizer']['name'])) {
$entity->panelizer = $this
->get_default_panelizer_object($bundle, $form_state['values']['panelizer']['name']);
$entity->panelizer->did = NULL;
$entity->panelizer->entity_id = $form['panelizer']['#entity_id'];
$entity->panelizer->revision_id = $form['panelizer']['#revision_id'];
}
}
public function entity_allows_revisions($entity) {
$retval = array();
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
$node_options = variable_get('node_options_' . $bundle, array(
'status',
'promote',
));
$retval[0] = in_array('revision', $node_options);
$retval[1] = user_access('administer nodes');
}
public function clone_panelizer($panelizer, $entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
$panelizer_clone = clone $panelizer;
$code = panels_export_display($panelizer->display);
ob_start();
eval($code);
ob_end_clean();
$panelizer_clone->display = $display;
$panelizer_clone->did = NULL;
$panelizer_clone->name = NULL;
$panelizer_clone->entity_type = $this->entity_type;
$panelizer_clone->entity_id = $entity_id;
$panelizer_clone->revision_id = (int) $revision_id;
return $panelizer_clone;
}
function access_admin($entity, $op = NULL) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
if (!$this
->is_panelized($bundle)) {
return FALSE;
}
if ($op && $op != 'settings' && empty($entity->panelizer)) {
return FALSE;
}
return $this
->panelizer_access($op, $entity) && $this
->entity_access('update', $entity);
}
function panelizer_access($op, $entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
return user_access('administer panelizer') || user_access("administer panelizer {$this->entity_type} {$bundle} {$op}");
}
function page_settings($js, $input, $entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
if (empty($entity->panelizer)) {
if ($this
->has_default_panel($bundle)) {
return MENU_NOT_FOUND;
}
$form_id = 'panelizer_panelize_entity_form';
$panelizer = $this
->get_internal_default_panelizer($bundle);
$panelizer->name = NULL;
}
else {
$form_id = 'panelizer_settings_form';
$reset_button = TRUE;
$panelizer = $entity->panelizer;
}
$form_state = array(
'entity' => $entity,
'revision info' => $this
->entity_allows_revisions($entity),
'panelizer' => $panelizer,
'no_redirect' => TRUE,
);
if (!empty($reset_button)) {
$form_state['reset button'] = TRUE;
}
ctools_include('common', 'panelizer');
$output = drupal_build_form($form_id, $form_state);
if (!empty($form_state['executed'])) {
if (empty($form_state['clicked_button']['#reset'])) {
drupal_set_message(t('The settings have been updated.'));
$entity->panelizer = $form_state['panelizer'];
if (empty($panelizer->did) || !empty($panelizer->name)) {
$panelizer->display_is_modified = TRUE;
}
$this
->entity_save($entity);
}
else {
$this
->delete_entity_panelizer($entity);
}
drupal_goto($_GET['q']);
}
return $output;
}
function page_context($js, $input, $entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
$cache_key = $entity_id;
$panelizer = panelizer_context_cache_get($this->entity_type, $cache_key);
if (empty($panelizer)) {
return MENU_NOT_FOUND;
}
$form_state = array(
'panelizer' => &$panelizer,
'entity' => $entity,
'revision info' => $this
->entity_allows_revisions($entity),
'panelizer type' => $this->entity_type,
'cache key' => $cache_key,
'no_redirect' => TRUE,
);
ctools_include('common', 'panelizer');
$output = drupal_build_form('panelizer_default_context_form', $form_state);
if (!empty($form_state['executed'])) {
if (!empty($form_state['clicked_button']['#write'])) {
drupal_set_message(t('The settings have been updated.'));
$entity->panelizer = $form_state['panelizer'];
$this
->entity_save($entity);
}
else {
drupal_set_message(t('Changes have been discarded.'));
}
panelizer_context_cache_clear($this->entity_type, $cache_key);
drupal_goto($_GET['q']);
}
return $output;
}
function page_layout($js, $input, $entity, $step = NULL, $layout = NULL) {
$panelizer = $entity->panelizer;
if (empty($panelizer)) {
return MENU_NOT_FOUND;
}
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
$display = $panelizer->display;
$display->context = $this
->get_contexts($panelizer, $entity);
$bits = explode('/', $this->plugin['entity path']);
foreach ($bits as $count => $bit) {
if (strpos($bit, '%') === 0) {
$bits[$count] = $entity_id;
}
}
$bits[] = 'panelizer';
$finish_path = $bits;
$bits[] = 'layout';
$bits[] = '%step';
$form_state = array(
'entity' => $entity,
'revision info' => $this
->entity_allows_revisions($entity),
'display' => $display,
'wizard path' => implode('/', $bits),
'allowed_layouts' => 'panelizer_' . $this->entity_type . ':' . $bundle,
);
ctools_include('common', 'panelizer');
$output = panelizer_change_layout_wizard($form_state, $step, $layout);
if (!empty($form_state['complete'])) {
$entity->panelizer->display = $form_state['display'];
$entity->panelizer->display_is_modified = TRUE;
$this
->entity_save($entity);
drupal_set_message(t('The layout has been changed.'));
drupal_goto(implode('/', $finish_path) . '/content');
}
return $output;
}
function page_content($js, $input, $entity) {
$panelizer = $entity->panelizer;
if (empty($panelizer)) {
return MENU_NOT_FOUND;
}
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
$form_state = array(
'entity' => $entity,
'revision info' => $this
->entity_allows_revisions($entity),
'display cache' => panels_edit_cache_get(implode(':', array(
'panelizer',
$this->entity_type,
$entity_id,
))),
'no_redirect' => TRUE,
);
ctools_include('common', 'panelizer');
$output = drupal_build_form('panelizer_edit_content_form', $form_state);
if (!empty($form_state['executed'])) {
if (!empty($form_state['clicked_button']['#save-display'])) {
drupal_set_message(t('The settings have been updated.'));
$entity->panelizer->display = $form_state['display'];
$entity->panelizer->display_is_modified = TRUE;
$this
->entity_save($entity);
}
else {
drupal_set_message(t('Changes have been discarded.'));
}
panels_edit_cache_clear($form_state['display cache']);
drupal_goto($_GET['q']);
}
ctools_set_no_blocks(FALSE);
drupal_set_page_content($output);
$page = element_info('page');
return $page;
}
function delete_entity_panelizer($entity) {
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
$dids = db_query("SELECT did FROM {panelizer_entity} WHERE entity_type = '{$this->entity_type}' AND entity_id = :id", array(
':id' => $entity_id,
))
->fetchCol();
foreach (array_unique($dids) as $did) {
panels_delete_display($did);
}
db_delete('panelizer_entity')
->condition('entity_type', $this->entity_type)
->condition('entity_id', $entity_id)
->execute();
}
public function is_panelized($bundle) {
return !empty($this->plugin['bundles'][$bundle]) && !empty($this->plugin['bundles'][$bundle]['status']);
}
public function has_default_panel($bundle) {
return $this
->is_panelized($bundle) && !empty($this->plugin['bundles'][$bundle]['default']);
}
public function has_panel_choice($bundle) {
return !empty($this->plugin['bundles'][$bundle]['choice']);
}
public function load_default_panelizer_objects($names) {
ctools_include('export');
$panelizers = ctools_export_load_object('panelizer_defaults', 'names', $names);
return $panelizers;
}
public function get_default_panelizer_objects($bundle) {
$conditions = array(
'panelizer_type' => $this->entity_type,
'panelizer_key' => $bundle,
);
ctools_include('export');
return ctools_export_load_object('panelizer_defaults', 'conditions', $conditions);
}
public function access_default_panelizer_object($panelizer) {
if (user_access('administer panelizer')) {
return TRUE;
}
ctools_include('context');
return user_access("administer panelizer {$this->entity_type} {$panelizer->panelizer_key} defaults") && ctools_access($panelizer->access, $this
->get_contexts($panelizer));
}
public function hook_panelizer_defaults(&$panelizers) {
$default_names =& drupal_static('panelizer_defaults_in_database', NULL);
if (!isset($default_names)) {
$default_names = drupal_map_assoc(db_query("SELECT name FROM {panelizer_defaults} WHERE name LIKE '%:default'")
->fetchCol());
}
foreach ($this->plugin['bundles'] as $bundle => $info) {
if (!empty($info['status']) && !empty($info['default'])) {
$panelizer = $this
->get_internal_default_panelizer($bundle);
if (empty($default_names[$panelizer->name]) && !isset($panelizers[$panelizer->name])) {
$panelizers[$panelizer->name] = $panelizer;
}
}
}
}
public function get_internal_default_panelizer($bundle) {
ctools_include('export');
$load_name = implode(':', array(
$this->entity_type,
$bundle,
'default',
));
$panelizer = ctools_export_crud_new('panelizer_defaults');
$panelizer->panelizer_type = $this->entity_type;
$panelizer->panelizer_key = $bundle;
$panelizer->name = $load_name;
$panelizer->display = $this
->get_default_display();
$panelizer->api_version = 1;
$panelizer->title = t('Default');
return $panelizer;
}
public function get_default_panelizer_object($bundle, $name) {
if (strpos($name, ':') === FALSE) {
$name = implode(':', array(
$this->entity_type,
$bundle,
$name,
));
}
ctools_include('export');
return ctools_export_crud_load('panelizer_defaults', $name);
}
function get_default_display() {
$display = panels_new_display();
$display->layout = 'flexible';
return $display;
}
function get_panelizer_object($entity_id) {
}
public function settings_form(&$form, &$form_state) {
$form['entities'][$this->entity_type] = array(
'#theme' => 'panelizer_settings_page_table',
'#header' => array(
array(
'data' => $this
->entity_bundle_label(),
'width' => '15%',
),
t('Panelize'),
t('Provide default panel'),
t('Allow panel choice'),
array(
'data' => t('Operations'),
'width' => '50%',
),
),
'#columns' => array(
'title',
'status',
'default',
'choice',
'links',
),
);
$entity_info = entity_get_info($this->entity_type);
$bundles = $entity_info['bundles'];
drupal_alter('panelizer_default_types', $bundles, $this->entity_type);
foreach ($bundles as $bundle => $bundle_info) {
$base_id = str_replace(array(
'][',
'_',
' ',
), '-', '#edit-entities-' . $this->entity_type . '-' . $bundle);
$base_url = 'admin/config/content/panelizer/' . $this->entity_type . '/' . $bundle;
$settings = !empty($this->plugin['bundles'][$bundle]) ? $this->plugin['bundles'][$bundle] : array(
'status' => FALSE,
'default' => FALSE,
'choice' => FALSE,
);
$form['entities'][$this->entity_type][$bundle]['title'] = array(
'#markup' => $bundle_info['label'],
);
$form['entities'][$this->entity_type][$bundle]['status'] = array(
'#type' => 'checkbox',
'#default_value' => !empty($settings['status']),
);
$form['entities'][$this->entity_type][$bundle]['default'] = array(
'#type' => 'checkbox',
'#default_value' => !empty($settings['default']),
'#states' => array(
'visible' => array(
$base_id . '-status' => array(
'checked' => TRUE,
),
),
),
);
$form['entities'][$this->entity_type][$bundle]['choice'] = array(
'#type' => 'checkbox',
'#default_value' => !empty($settings['choice']),
'#states' => array(
'visible' => array(
$base_id . '-status' => array(
'checked' => TRUE,
),
),
),
);
$form['entities'][$this->entity_type][$bundle]['links'] = array(
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
);
if (!empty($settings['status'])) {
$links_array = array(
'settings' => array(
'title' => t('allowed content'),
'href' => $base_url . '/allowed',
),
);
$links = theme('links', array(
'links' => $links_array,
'attributes' => array(
'class' => 'links inline',
),
));
}
else {
$links = t('Save to access allowed content');
}
$form['entities'][$this->entity_type][$bundle]['links']['basic'] = array(
'#type' => 'item',
'#title' => $links,
'#states' => array(
'visible' => array(
$base_id . '-status' => array(
'checked' => TRUE,
),
),
),
);
if (!empty($settings['status']) && !empty($settings['default']) && empty($settings['choice'])) {
$links_array = array(
'settings' => array(
'title' => t('settings'),
'href' => $base_url . '/settings',
),
'context' => array(
'title' => t('context'),
'href' => $base_url . '/context',
),
'layout' => array(
'title' => t('layout'),
'href' => $base_url . '/layout',
),
'content' => array(
'title' => t('content'),
'href' => $base_url . '/content',
),
);
$links = theme('links', array(
'links' => $links_array,
'attributes' => array(
'class' => 'links inline',
),
));
}
else {
$links = t('Save to access default panel');
}
$form['entities'][$this->entity_type][$bundle]['links']['default'] = array(
'#type' => 'item',
'#title' => $links,
'#states' => array(
'visible' => array(
$base_id . '-status' => array(
'checked' => TRUE,
),
$base_id . '-default' => array(
'checked' => TRUE,
),
$base_id . '-choice' => array(
'checked' => FALSE,
),
),
),
);
if (!empty($settings['status']) && !empty($settings['choice'])) {
$links_array = array(
'list' => array(
'title' => t('list'),
'href' => $base_url . '/list',
),
);
$links = theme('links', array(
'links' => $links_array,
'attributes' => array(
'class' => 'links inline',
),
));
}
else {
$links = t('Save to access panel list');
}
$form['entities'][$this->entity_type][$bundle]['links']['default2'] = array(
'#type' => 'item',
'#title' => $links,
'#states' => array(
'visible' => array(
$base_id . '-status' => array(
'checked' => TRUE,
),
$base_id . '-choice' => array(
'checked' => TRUE,
),
),
),
);
}
}
public function settings_form_validate(&$form, &$form_state) {
}
public function settings_form_submit(&$form, &$form_state) {
if (empty($form_state['values']['entities'][$this->entity_type])) {
return;
}
foreach ($form_state['values']['entities'][$this->entity_type] as $bundle => $values) {
variable_set('panelizer_defaults_' . $this->entity_type . '_' . $bundle, $values);
}
}
function render_entity($entity, $args = array(), $address = NULL) {
if (empty($entity->panelizer) || empty($entity->panelizer->display)) {
return FALSE;
}
list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
$panelizer = $entity->panelizer;
$display = $entity->panelizer->display;
$display->context = $this
->get_contexts($panelizer, $entity);
$display->args = $args;
$display->css_id = $panelizer->css_id;
$display->cache_key = implode(':', array(
'panelizer',
$this->entity_type,
$entity_id,
));
if (!empty($panelizer->css)) {
ctools_include('css');
$filename = ctools_css_retrieve($display->cache_key);
if (!$filename) {
$filename = ctools_css_store($display->cache_key, $panelizer->css);
}
drupal_add_css($filename, array(
'group' => CSS_THEME,
));
}
panels_get_current_page_display($display);
ctools_include('plugins', 'panels');
$renderer = panels_get_renderer($panelizer->pipeline, $display);
$renderer->address = $address;
$info = array(
'content' => panels_render_display($display, $renderer),
'no_blocks' => !empty($panelizer->no_blocks),
);
return $info;
}
public function get_contexts($panelizer, $entity = NULL) {
ctools_include('context');
if (empty($panelizer->base_contexts)) {
$panelizer->base_contexts = $this
->get_base_contexts($entity);
}
$contexts = ctools_context_load_contexts($panelizer);
return $contexts;
}
public function get_base_contexts($entity = NULL) {
ctools_include('context');
if ($entity) {
$context = ctools_context_create('entity:' . $this->entity_type, $entity);
}
else {
$context = ctools_context_create_empty('entity:' . $this->entity_type);
$context->placeholder = array(
'type' => 'context',
'conf' => array(
'name' => $this->entity_type,
'identifier' => $this
->entity_identifier($entity),
'keyword' => $this->entity_type,
'context_settings' => array(),
),
);
}
$context->identifier = $this
->entity_identifier($entity);
$context->keyword = $this->entity_type;
return array(
'panelizer' => $context,
);
}
public function entity_identifier($entity) {
$entity_info = entity_get_info($this->entity_type);
return t('This @entity', array(
'@entity' => $entity_info['label'],
));
}
public function get_bundle_title($bundle) {
$entity_info = entity_get_info($this->entity_type);
return isset($entity_info['bundles'][$bundle]['label']) ? $entity_info['bundles'][$bundle]['label'] : '';
}
public function entity_bundle_label() {
$entity_info = entity_get_info($this->entity_type);
return t('@entity bundle', array(
'@entity' => $entity_info['label'],
));
}
public function hook_views_data_alter(&$items) {
$entity_info = entity_get_info($this->entity_type);
if (!empty($entity_info['base table'])) {
$table = $entity_info['base table'];
$items[$table]['panelizer_link'] = array(
'field' => array(
'title' => t('Panelizer link'),
'help' => t('Provide a link to panelizer-related operations on the content.'),
'handler' => 'panelizer_handler_field_link',
'entity_type' => $this->entity_type,
),
);
$items[$table]['panelizer_status'] = array(
'field' => array(
'title' => t('Panelizer status'),
'help' => t('Display whether an entity is panelized and which panelizer option it is using.'),
'handler' => 'panelizer_handler_panelizer_status',
'entity_type' => $this->entity_type,
),
);
if (!empty($entity_info['entity keys']['revision'])) {
$id_field = $entity_info['entity keys']['revision'];
$field = 'revision_id';
}
else {
$id_field = $entity_info['entity keys']['id'];
$field = 'entity_id';
}
$items['panelizer_entity_' . $table]['table']['join'] = array(
$table => array(
'handler' => 'views_join',
'table' => 'panelizer_entity',
'left_table' => $table,
'left_field' => $id_field,
'field' => $field,
'extra' => array(
array(
'field' => 'entity_type',
'value' => $this->entity_type,
'operator' => '=',
),
),
),
);
$items['panelizer_entity_' . $table]['table']['group'] = $items[$table]['table']['group'];
$items['panelizer_entity_' . $table]['name'] = array(
'filter' => array(
'title' => t('Panelizer status'),
'help' => t('Filter based upon panelizer status.'),
'handler' => 'panelizer_handler_filter_panelizer_status',
'entity_type' => $this->entity_type,
),
);
}
}
}
function panelizer_entity_default_bundle_form_submit($form, &$form_state) {
$bundle = $form['panelizer']['#bundle'];
$type_location = $form['panelizer']['#location'];
$form_state['panelizer_entity_handler']
->add_bundle_setting_form_submit($form, $form_state, $bundle, $type_location);
}