View source
<?php
class ContentTypeGroupUIController extends EntityDefaultUIController {
public function hook_menu() {
$items = parent::hook_menu();
$wildcard = '%content_type_group';
$items[$this->path] = array(
'title' => 'Content type groups',
'description' => 'Manage content type groups',
'page callback' => 'content_type_groups_admin',
'file' => 'content_type_groups.admin.inc',
'file path' => drupal_get_path('module', $this->entityInfo['module']),
'access arguments' => array(
'administer content types',
),
'type' => MENU_LOCAL_ACTION,
);
$items[$this->path . '/add'] = array(
'title' => 'Add content type group',
'description' => 'Add a new content type group',
'page callback' => 'content_type_groups_admin_add_page',
'file' => 'content_type_groups.admin.inc',
'file path' => drupal_get_path('module', $this->entityInfo['module']),
'type' => MENU_LOCAL_ACTION,
'access arguments' => array(
'administer content types',
),
);
$items[$this->path . '/' . $wildcard . '/edit'] = array(
'title' => 'Edit content type group',
'description' => 'Edit an existing content type group',
'load arguments' => array(
TRUE,
),
'page callback' => 'entity_ui_get_form',
'page arguments' => array(
'content_type_group',
4,
),
'file' => 'content_type_groups.admin.inc',
'file path' => drupal_get_path('module', $this->entityInfo['module']),
'access arguments' => array(
'administer content types',
),
);
$items[$this->path . '/' . $wildcard . '/delete'] = array(
'title' => 'Delete content type group',
'description' => 'Delete an existing content type group',
'load arguments' => array(
TRUE,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'content_type_group_form_delete_confirm',
4,
),
'file' => 'content_type_groups.admin.inc',
'file path' => drupal_get_path('module', $this->entityInfo['module']),
'access arguments' => array(
'administer content types',
),
);
return $items;
}
}
class ContentTypeGroupController extends EntityAPIController {
public function create(array $values = array()) {
$values += array(
'type' => NULL,
'name' => '',
'content_types' => array(),
);
return parent::create($values);
}
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
$query
->leftJoin('content_type_groups_types', 't', 'base.type = t.group_type');
$query
->addField('t', 'content_type', 'content_type');
$query
->addField('t', 'weight', 'weight');
return $query;
}
public function load($ids = array(), $conditions = array()) {
$entities = array();
if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
$revision_id = $conditions[$this->revisionKey];
unset($conditions[$this->revisionKey]);
}
else {
$revision_id = FALSE;
}
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
if ($this->cache && !$revision_id) {
$entities = $this
->cacheGet($ids, $conditions);
if ($passed_ids) {
$ids = array_keys(array_diff_key($passed_ids, $entities));
}
}
if (!empty($this->entityInfo['entity cache']) && !$revision_id && $ids && !$conditions) {
$cached_entities = EntityCacheControllerHelper::entityCacheGet($this, $ids, $conditions);
$ids = array_diff($ids, array_keys($cached_entities));
$entities += $cached_entities;
if ($this->cache && !empty($cached_entities) && !$revision_id) {
$this
->cacheSet($cached_entities);
}
}
if (!($this->cacheComplete && $ids === FALSE && !$conditions) && ($ids === FALSE || $ids || $revision_id)) {
$queried_entities = array();
foreach ($this
->query($ids, $conditions, $revision_id) as $record) {
if (isset($entities[$record->{$this->idKey}])) {
continue;
}
if (!empty($this->entityInfo['base table'])) {
$schema = drupal_get_schema($this->entityInfo['base table']);
foreach ($schema['fields'] as $field => $info) {
if (!empty($info['serialize']) && isset($record->{$field})) {
$record->{$field} = unserialize($record->{$field});
if (!empty($info['merge']) && is_array($record->{$field})) {
foreach ($record->{$field} as $key => $value) {
$record->{$key} = $value;
}
unset($record->{$field});
}
}
}
}
$all_node_types = node_type_get_names();
if (isset($queried_entities[$record->{$this->idKey}])) {
$queried_entities[$record->{$this->idKey}]->content_types[$record->content_type] = array(
'name' => $all_node_types[$record->content_type],
'#weight' => $record->weight,
);
}
else {
$content_type = array(
'name' => $all_node_types[$record->content_type],
'#weight' => $record->weight,
);
$record->content_types = array(
$record->content_type => $content_type,
);
unset($record->content_type);
unset($record->weight);
$queried_entities[$record->{$this->idKey}] = $record;
}
}
}
if (!empty($queried_entities)) {
$this
->attachLoad($queried_entities, $revision_id);
$entities += $queried_entities;
}
if (!empty($this->entityInfo['entity cache']) && !empty($queried_entities) && !$revision_id) {
EntityCacheControllerHelper::entityCacheSet($this, $queried_entities);
}
if ($this->cache) {
if (!empty($queried_entities) && !$revision_id) {
$this
->cacheSet($queried_entities);
if (!$conditions && $ids === FALSE) {
$this->cacheComplete = TRUE;
}
}
}
if ($passed_ids && ($passed_ids = array_intersect_key($passed_ids, $entities))) {
foreach ($passed_ids as $id => $value) {
$passed_ids[$id] = $entities[$id];
}
$entities = $passed_ids;
}
return $entities;
}
}
class ContentTypeGroup extends Entity {
protected function defaultLabel() {
return $this->name;
}
protected function defaultUri() {
return array(
'path' => CONTENT_TYPE_GROUPS_ADMIN_PATH . '/manage/' . $this
->identifier(),
);
}
}
function content_type_groups_entity_presave($entity, $entity_type) {
if ($entity_type == CONTENT_TYPE_GROUPS_ENTITY_NAME) {
foreach ($entity->content_types as $content_type => $checked) {
if (!$checked) {
unset($entity->content_types[$content_type]);
}
}
}
}
function content_type_gorups_entity_insert($entity, $entity_type) {
if ($entity_type == CONTENT_TYPE_GROUPS_ENTITY_NAME) {
dpm($entity);
}
}