Skip to content

Commit

Permalink
ECK 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell committed Dec 7, 2011
1 parent 0e499bf commit ca81c17
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 106 deletions.
71 changes: 70 additions & 1 deletion eck.entity.inc
Expand Up @@ -377,15 +377,39 @@ function eck__entity__form($form, $form_state, $entity) {
'#value' => $entity
);

// Property Widget Handling
// Property Widget Handling through property_info by entity api
$property_info = entity_get_property_info($entity->entityType());
$properties = array();
$found_widget = FALSE;
foreach($property_info['properties'] as $pname => $pi){
if(array_key_exists('widget', $pi)){
$widget_callback = $pi['widget'];
$widget = $widget_callback($entity);
$properties[$pname] = $widget_callback;
$form[$pname] = $widget_callback($entity);
$found_widget = TRUE;
}
}

if(!$found_widget){
//If there was no widget given through the property_info array, we look for
//a widget in the property behaviors implemented
$entity_type = $entity->entityType();
$entity_type = EntityType::loadByName($entity_type);
$properties = $entity_type->properties;

foreach($properties as $property => $info){
//If there is a behavior associated with this property we need to call the appropiate hooks
if(array_key_exists('behavior', $info) && !empty($info['behavior'])){
$behavior = $info['behavior'];

$plugin = ctools_get_plugins('eck', 'property_behavior', $behavior);

$widget = _eck_apply_plugin_widget($plugin, $entity, $property);
if($widget){
$form[$property] = $widget;
}
}
}
}

Expand Down Expand Up @@ -432,6 +456,7 @@ function eck__entity__form_submit($form, &$state) {
}
}
}
//dpm($entity, "Before Save");

$entity->save();

Expand All @@ -456,6 +481,28 @@ function _eck_form_property_value($state, $property){
return NULL;
}

function _eck_apply_plugin_widget($plugin, $entity, $property){

//Now we get the funtions from the plugin to handle save, insert and update
$function = ctools_plugin_get_function($plugin, 'default_widget');

if($function){
return $function($entity, $property);
}
return NULL;
}

function _eck_apply_plugin_formatter($plugin, $entity, $property){

//Now we get the funtions from the plugin to handle save, insert and update
$function = ctools_plugin_get_function($plugin, 'default_formatter');

if($function){
return $function($entity, $property);
}
return NULL;
}


/**
* Creates a renderable array to show an entity
Expand All @@ -468,11 +515,33 @@ function _eck_form_property_value($state, $property){
* (int) the Id of the entity to be deleted
*/
function eck__entity__view($entity_type_name, $bundle_name, $id) {
$entity = entity_load($entity_type_name, array($id));
$entity = $entity[$id];

$entity_type = entity_type_load($entity_type_name);
$bundle = bundle_load($entity_type_name, $bundle_name);

$build = array();
$entity_view = eck__entity__build($entity_type, $bundle, $id);

$properties = $entity_type->properties;
$property_view = array();
foreach($properties as $property => $info){
//If there is a behavior associated with this property we need to call the appropiate hooks
if(array_key_exists('behavior', $info) && !empty($info['behavior'])){
$behavior = $info['behavior'];

$plugin = ctools_get_plugins('eck', 'property_behavior', $behavior);
$formatter = _eck_apply_plugin_formatter($plugin, $entity, $property);

if($formatter){
$property_view[$property] = $formatter;
}
}
}

$entity_view[$entity->entityType()][$entity->id] = array_merge($property_view, $entity_view[$entity->entityType()][$entity->id]);

$build["{$entity_type->name}_{$bundle->name}_page"] = $entity_view;

return $build;
Expand Down
129 changes: 127 additions & 2 deletions eck.install
Expand Up @@ -115,7 +115,7 @@ function eck_schema() {
'not null' => TRUE
)
),
'primary key' => array('id', 'machine_name'),
'primary key' => array('id'/*, 'machine_name'*/), //sad.. taken this out until I figure out how to make it work
'indexes' => array(
/*'entity_type_bundle' => array('entity_type', 'name'),*/ //Nope, Don't need it
),
Expand Down Expand Up @@ -330,9 +330,134 @@ function eck_update_7005() {
function eck_update_7006() {
//lets start with then entity type table

//first lets take the properties and translate them to the new form.
//lets add an id field to the entity_type table, and set it up as a primary key
//db_add_primary_key('eck_entity_type', array('id'));
db_drop_primary_key('eck_entity_type');

db_add_field('eck_entity_type', 'id', array(
'description' => "The primary identifier for an entity type",
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
), array('primary key' => array('id', 'name')));

//lets take the properties and translate them to the new form.
$results = db_select('eck_entity_type', 't')->fields('t')->execute();

foreach ($results as $record) {

//@todo for each entity type we want to drop all the indexes.. they will have to be managed dynamically by the
//behaviors even though I don't know how to do that yet
$entity_table = "eck_{$record->name}";

if(db_index_exists($entity_table, 'uid')){
db_drop_index($entity_table, 'uid');
}
if(db_index_exists($entity_table, "{$record->name}_created")){
db_drop_index($entity_table, "{$record->name}_created");
}
if(db_index_exists($entity_table, "{$record->name}_changed")){
db_drop_index($entity_table, "{$record->name}_changed");
}

$new_properties = array();

$properties = $record->properties;
$properties = unserialize($properties);
foreach($properties as $property => $status){
switch($property){
case 'uuid':
$new_properties['uuid'] = array('label' => 'UUID', 'type' => 'uuid');
break;

case 'uid':
$new_properties['uid'] = array('label' => 'Author', 'type' => 'integer', 'behavior' => 'author');
break;

case 'created':
$new_properties['created'] = array('label' => 'Created', 'type' => 'integer', 'behavior' => 'created');
break;

case 'changed':
$new_properties['changed'] = array('label' => 'Changed', 'type' => 'integer', 'behavior' => 'changed');
break;

case 'state':
$new_properties['state'] = array('label' => 'State', 'type' => 'positive_integer');
break;
}
}

$custom_properties = $record->custom_properties;
$custom_properties = unserialize($custom_properties);

foreach($custom_properties as $property => $info){

$type = $info['type'];
$label = $info['label'];
switch($type){
case 'text':
$new_properties[$property] = array('label' => $label, 'type' => $type);
break;

case 'decimal':
$new_properties[$property] = array('label' => $label, 'type' => $type);
break;

default:
$new_properties[$property] = array('label' => $label, 'type' => 'integer');
break;
}
}

$encode_new = drupal_json_encode($new_properties);

db_update('eck_entity_type') // Table name no longer needs {}
->fields(array(
'properties' => $encode_new
))
->condition('name', $record->name, '=')
->execute();
}

//Now we can drop the custom_properties field now that everything has been moved
db_drop_field('eck_entity_type', 'custom_properties');

//now the changes to the eck_bundle table

//first lets drop that index
db_drop_index('eck_bundle', 'entity_type_bundle_name');

db_add_field('eck_bundle', 'machine_name', array(
'description' => "A combination of the entity type and the name of
this bundle, this combination is unique",
'type' => 'varchar',
'length' => 128,
'default' =>"",
'not null' => TRUE
));

//


}

function eck_update_7007() {

//lets generate the machine names for each bundle
$results = db_select('eck_bundle', 't')->fields('t')->execute();

foreach ($results as $record) {
$name = $record->name;
$entity_type = $record->entity_type;
$record->machine_name = "{$entity_type}_{$name}";
db_update('eck_bundle') // Table name no longer needs {}
->fields(array(
'machine_name' => $record->machine_name
))
->condition('id', $record->id, '=')
->execute();
}
}


Expand Down
9 changes: 7 additions & 2 deletions eck.properties.inc
Expand Up @@ -3,6 +3,8 @@
function eck_set_properties_schema(&$schema, $entity_type){
$properties = $entity_type->properties;

//dpm($properties, "Properties in schema");

foreach($properties as $name => $info){
$type = $info['type'];
$schema['fields'][$name] = eck_property_type_schema($type);
Expand Down Expand Up @@ -298,14 +300,17 @@ function eck__properties__form_submit($form, &$state){
}
else if($state['values']['op'] == "Save"){
//Here we want to add the properties to the entity type and save it
dpm($state, "State");
$entity_type = $state['values']['entity_type'];
//dpm($entity_type, "Entity Type");

foreach($state['values']['new_properties_table'] as $property => $active){
if($active){
$info = $state['values']['new_properties'][$property];
$entity_type->addProperty($property, $info['label'], $info['type'], $info['behavior']);
if(array_key_exists('behavior', $info)){
$entity_type->addProperty($property, $info['label'], $info['type'], $info['behavior']);
}else{
$entity_type->addProperty($property, $info['label'], $info['type']);
}
}else{
$entity_type->removeProperty($property);
}
Expand Down
4 changes: 4 additions & 0 deletions eck_spb/eck_spb.info
@@ -0,0 +1,4 @@
name = ECK Sample Property Behavior
description = A module exposing a simple property behavior, that allows us to set a property through an entities input form
package = ECK
core = 7.x
12 changes: 12 additions & 0 deletions eck_spb/eck_spb.module
@@ -0,0 +1,12 @@
<?php

function eck_spb_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'eck' && $plugin_type == 'property_behavior') {
//This is a folder relative to our module. So we are stroing our property behavior plugins in
//a folder called property_behavior which is the plugin type's name. In ECK we stared the plugins in a
//directory under the plugins directory, you can look at eck.module to see the differences in the
//hook_ctools_plugin_directory implementation.
return $plugin_type;
}
}

67 changes: 67 additions & 0 deletions eck_spb/property_behavior/eck_spb_simple_title.inc
@@ -0,0 +1,67 @@
<?php
/*
* In a property behavior plugin we want to hijack drupal when it is relavant. In this case we
* want to do it when an entity with a property using our behavior is being manipulated: created, updated, saved,
* shown, etc.
*
* So the plugin implementation in ECK gives an opportunity to do this in multiple places. Currelty this are the
* places where we can do it
*
* entity_save: save gets called everytime the entity is being saved.
*
* entity_insert: insert is a subset of save, in which it only gets called on a new entity
* entity_update: insert is a subset of save, in which it only gets called on an entity that was loaded (edit mode)
*
* We are also allowed to give a default_widget for this behavior, and a default_formatter
*
* As new use cases arise, I am sure that this would be expanded to many other cases.
*
* THE PLUGIN
*
* The plugin is a very simple array with a label, and with the different callbacks that we want to implement.
*
* In this example we are using the default_widget, default_formatter, and the save options.
* After the plugin array we give the functions that we want call at these different stages.
*/

$plugin = array(
'label' => "ECK SPB",
'entity_save' => 'eck_spb_entity_save',
'default_widget' => 'eck_spb_widget',
'default_formatter' => 'eck_spb_formatter'
);

function eck_spb_entity_save($entity, $property, $value = NULL){
$entity->{$property} = $value;
}

/**
* Define a rendarble array to input a value for the property using this behavior
*
* @param $entity: and entity object that contains the property for which this behavior is relevant
* @param $property: the name of the property (string) that is using this behavior
*
* @return a rendarable array use when the input form for this entity is displayed
*/
function eck_spb_widget($entity, $property){
$title = _eck_spb_extract_title($entity, $property);
return array(
'#type' => 'textfield',
'#title' => ucfirst($property),
'#default_value' => $title
);
}

function eck_spb_formatter($entity, $property){
$title = _eck_spb_extract_title($entity, $property);
return array('#markup' => "<h1>{$title}</h1>");
}

function _eck_spb_extract_title($entity, $property){
$title = "";
if(isset($entity->{$property})){
$title = $entity->{$property};
}

return $title;
}
8 changes: 0 additions & 8 deletions properties/README.txt

This file was deleted.

6 changes: 0 additions & 6 deletions properties/title/eck_property_title.info

This file was deleted.

0 comments on commit ca81c17

Please sign in to comment.