| 1 |
<?php |
<?php |
| 2 |
// $Id: text.module,v 1.3 2009/02/10 03:16:15 webchick Exp $ |
// $Id: combo.module,v 1.1 2009/09/05 13:41:30 bjaspan Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 129 |
} |
} |
| 130 |
|
|
| 131 |
/** |
/** |
| 132 |
|
* Implementation of hook_menu(). |
| 133 |
|
*/ |
| 134 |
|
function combo_menu() { |
| 135 |
|
$items = array(); |
| 136 |
|
|
| 137 |
|
$items['admin/structure/combofield'] = array( |
| 138 |
|
'title' => 'Combo fields', |
| 139 |
|
'page callback' => 'combo_overview_page', |
| 140 |
|
'access arguments' => array('administer content types'), |
| 141 |
|
'type' => MENU_NORMAL_ITEM, |
| 142 |
|
); |
| 143 |
|
|
| 144 |
|
foreach (field_info_fields() as $field) { |
| 145 |
|
if ($field['type'] == 'combo') { |
| 146 |
|
$urlstr = str_replace('_', '-', $field['field_name']); |
| 147 |
|
$items["admin/structure/combofield/$urlstr"] = array( |
| 148 |
|
'title' => $field['field_name'], |
| 149 |
|
'page callback' => 'combo_page', |
| 150 |
|
'page arguments' => array(3), |
| 151 |
|
'access arguments' => array('administer content types'), |
| 152 |
|
'type' => MENU_CALLBACK, |
| 153 |
|
); |
| 154 |
|
$items["admin/structure/combofield/$urlstr/edit"] = array( |
| 155 |
|
'title' => 'Edit', |
| 156 |
|
'type' => MENU_DEFAULT_LOCAL_TASK, |
| 157 |
|
); |
| 158 |
|
} |
| 159 |
|
} |
| 160 |
|
dpm('combo menu'); |
| 161 |
|
dpm($items); |
| 162 |
|
return $items; |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
function combo_overview_page() { |
| 166 |
|
$output = ''; |
| 167 |
|
foreach (field_info_fields() as $field) { |
| 168 |
|
if ($field['type'] == 'combo') { |
| 169 |
|
$output .= "<p>".l($field['field_name'], 'admin/structure/combofield/' . str_replace('_', '-', $field['field_name']))."</p>"; |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
return $output; |
| 173 |
|
} |
| 174 |
|
|
| 175 |
|
function combo_page($arg = NULL) { |
| 176 |
|
return "Hi!: $arg"; |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
/********************************************************************** |
| 180 |
|
* Entity API |
| 181 |
|
**********************************************************************/ |
| 182 |
|
|
| 183 |
|
/** |
| 184 |
* Implement hook_entity_info(). |
* Implement hook_entity_info(). |
| 185 |
*/ |
*/ |
| 186 |
function combo_entity_info() { |
function combo_entity_info() { |
| 187 |
$return = array( |
$return = array( |
| 188 |
'combo' => array( |
'combo' => array( |
| 189 |
'label' => t('Combo field'), |
'label' => t('Combo field'), |
| 190 |
|
'controller class' => 'ComboFieldController', |
| 191 |
'fieldable' => TRUE, |
'fieldable' => TRUE, |
| 192 |
'object keys' => array( |
'object keys' => array( |
| 193 |
'id' => 'id', |
'id' => 'id', |
| 200 |
); |
); |
| 201 |
|
|
| 202 |
// Every combo field name is a combo entity bundle. |
// Every combo field name is a combo entity bundle. |
| 203 |
foreach (field_info_fields() as $field) { |
$fields = field_read_fields(array('type' => 'combo')); |
| 204 |
|
foreach ($fields as $field) { |
| 205 |
if ($field['type'] == 'combo') { |
if ($field['type'] == 'combo') { |
| 206 |
$return['combo']['bundles'][$field['field_name']] = array( |
$return['combo']['bundles'][$field['field_name']] = array( |
| 207 |
'label' => $field['field_name'], |
'label' => $field['field_name'], |
| 208 |
|
'admin' => array( |
| 209 |
|
'path' => 'admin/structure/combofield/'. str_replace('_', '-', $field['field_name']), |
| 210 |
|
'access arguments' => array('administer content types'), |
| 211 |
|
), |
| 212 |
); |
); |
| 213 |
} |
} |
| 214 |
} |
} |
| 215 |
return $return; |
return $return; |
| 216 |
} |
} |
| 217 |
|
|
| 218 |
|
function combo_load($id) { |
| 219 |
|
return entity_load('combo', array($id => $id)); |
| 220 |
|
} |
| 221 |
|
|
| 222 |
|
class ComboFieldController implements DrupalEntityControllerInterface { |
| 223 |
|
/** |
| 224 |
|
* Constructor. |
| 225 |
|
* |
| 226 |
|
* @param $entityType |
| 227 |
|
* The entity type for which the instance is created. |
| 228 |
|
*/ |
| 229 |
|
public function __construct($entityType) { |
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
/** |
| 233 |
|
* Reset the internal, static entity cache. |
| 234 |
|
*/ |
| 235 |
|
public function resetCache() { |
| 236 |
|
} |
| 237 |
|
|
| 238 |
|
/** |
| 239 |
|
* Load one or more entities. |
| 240 |
|
* |
| 241 |
|
* @param $ids |
| 242 |
|
* An array of entity IDs, or FALSE to load all entities. |
| 243 |
|
* @param $conditions |
| 244 |
|
* An array of conditions in the form 'field' => $value. |
| 245 |
|
* |
| 246 |
|
* @return |
| 247 |
|
* An array of entity objects indexed by their ids. |
| 248 |
|
*/ |
| 249 |
|
public function load($ids = array(), $conditions = array()) { |
| 250 |
|
dpm('combo load'); |
| 251 |
|
dpm($ids); |
| 252 |
|
dpm($conditions); |
| 253 |
|
// TODO: not sure what "all entities" means here |
| 254 |
|
$entities = array(); |
| 255 |
|
foreach ($ids as $id) { |
| 256 |
|
// TODO: Questions: What is the vid? And how do I find out the |
| 257 |
|
// bundle of this entity == the combo field it is a delta of? |
| 258 |
|
// Without it, I cannot load sub-fields... |
| 259 |
|
$combo_entity = field_attach_create_stub_object('combo', array($id, NULL, 'combo1')); |
| 260 |
|
$entites[$id] = $combo_entity; |
| 261 |
|
} |
| 262 |
|
return $entities; |
| 263 |
|
} |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
/********************************************************************** |
| 267 |
|
* Field Type API |
| 268 |
|
**********************************************************************/ |
| 269 |
|
|
| 270 |
/** |
/** |
| 271 |
* Implementation of hook_field_info(). |
* Implementation of hook_field_info(). |
| 272 |
*/ |
*/ |
| 304 |
// Our combo data items are loaded, but still serialized. |
// Our combo data items are loaded, but still serialized. |
| 305 |
foreach ($combo_items[$obj_id] as $combo_delta => &$combo_item) { |
foreach ($combo_items[$obj_id] as $combo_delta => &$combo_item) { |
| 306 |
$combo_item['deltas'] = unserialize($combo_item['deltas']); |
$combo_item['deltas'] = unserialize($combo_item['deltas']); |
|
} |
|
| 307 |
|
|
| 308 |
// Create the combo entity for $field on $object. |
// Create the combo entity for $field/$combo_delta on $object. |
| 309 |
// TODO: Why do we need to pass $instance here? |
$combo_entity = _combo_create_entity($obj_type, $object, $combo_field, $combo_delta); |
|
list($obj_id) = field_attach_extract_ids($obj_type, $object); |
|
|
$combo_entity = _combo_create_entity($obj_type, $object, $combo_field, $combo_instances[$obj_id]); |
|
| 310 |
|
|
| 311 |
// Load field data for the combo entity, which is all the fields |
// Load field data for the combo entity, which is all the fields |
| 312 |
// attached to the combo field. |
// attached to the combo field. |
| 313 |
field_attach_load('combo', array($combo_entity->id => $combo_entity), $age, $options); |
field_attach_load('combo', array($combo_entity->id => $combo_entity), $age, $options); |
| 314 |
|
|
| 315 |
// Collate each $combo_entity field's data into $combo_items, which is |
// Collate each $combo_entity field's data into $combo_items, which is |
| 316 |
// what will end up on $object->{combo_field_name}. |
// what will end up on $object->{combo_field_name}. |
| 317 |
$entity_instances = field_info_instances($combo_field['field_name']); |
$entity_instances = field_info_instances($combo_field['field_name']); |
| 318 |
foreach ($entity_instances as $entity_instance) { |
foreach ($entity_instances as $entity_instance) { |
|
foreach ($combo_items[$obj_id] as $combo_delta => &$combo_item) { |
|
| 319 |
// It is possible this combo item will have no data for one of |
// It is possible this combo item will have no data for one of |
| 320 |
// the sub-fields. |
// the sub-fields. |
| 321 |
// TODO: The extra $combo_deltas here is unnecessary, a |
// TODO: The extra $combo_deltas here is unnecessary, a |
| 347 |
$entity_instances = field_info_instances($field['field_name']); |
$entity_instances = field_info_instances($field['field_name']); |
| 348 |
foreach ($items as $combo_delta => &$combo_item) { |
foreach ($items as $combo_delta => &$combo_item) { |
| 349 |
// Create the combo entity for $field/$combo_delta on $object |
// Create the combo entity for $field/$combo_delta on $object |
| 350 |
$combo_entity = _combo_create_entity($obj_type, $object, $field, $instance); |
$combo_entity = _combo_create_entity($obj_type, $object, $field, $combo_delta); |
| 351 |
|
|
| 352 |
$item_deltas = array(); |
$item_deltas = array(); |
| 353 |
|
|
| 382 |
} |
} |
| 383 |
} |
} |
| 384 |
|
|
| 385 |
|
function combo_field_is_empty($item, $field) { |
| 386 |
|
dpm('field is empty?'); |
| 387 |
|
dpm($item); |
| 388 |
|
return FALSE; |
| 389 |
|
} |
| 390 |
|
|
| 391 |
/** |
/** |
| 392 |
* Create a combo entity whose id is based on the object the combo |
* Create a combo entity whose id is based on the object the combo |
| 393 |
* field is attached to and whose bundle is the name of the combo |
* field is attached to and whose bundle is the name of the combo |
| 394 |
* field. |
* field. |
| 395 |
*/ |
*/ |
| 396 |
function _combo_create_entity($obj_type, $object, $field, $instance) { |
function _combo_create_entity($obj_type, $object, $field, $delta) { |
|
list($obj_id) = field_attach_extract_ids($obj_type, $object); |
|
|
$combo_id = _combo_get_id($obj_type, $object, $field, $instance); |
|
|
$combo_vid = _combo_get_vid($obj_type, $object, $field, $instance); |
|
| 397 |
$combo_bundle = $field['field_name']; |
$combo_bundle = $field['field_name']; |
| 398 |
$combo_entity = field_attach_create_stub_object('combo', array($combo_id, $combo_vid, $combo_bundle)); |
if (isset($object)) { |
| 399 |
|
list($obj_id) = field_attach_extract_ids($obj_type, $object); |
| 400 |
|
$combo_id = combo_atomize("{$obj_type}-{$obj_id}-{$delta}"); |
| 401 |
|
// TODO |
| 402 |
|
$combo_vid = NULL; |
| 403 |
|
$combo_entity = field_attach_create_stub_object('combo', array($combo_id, $combo_vid, $combo_bundle)); |
| 404 |
|
} |
| 405 |
|
else { |
| 406 |
|
$combo_entity = field_attach_create_stub_object('combo', array(NULL, NULL, $combo_bundle)); |
| 407 |
|
} |
| 408 |
|
|
| 409 |
return $combo_entity; |
return $combo_entity; |
| 410 |
} |
} |
| 411 |
|
|
|
/** |
|
|
* Create or retrieve a unique id for the combo entity for a combo |
|
|
* field on an object type. |
|
|
*/ |
|
|
function _combo_get_id($obj_type, $object, $field, $instance) { |
|
|
list($obj_id) = field_attach_extract_ids($obj_type, $object); |
|
|
return combo_atomize("{$obj_type}-id-{$obj_id}"); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Create or retrieve a unique revision id for the combo entity for a combo |
|
|
* field on an object type. |
|
|
*/ |
|
|
function _combo_get_vid($obj_type, $object, $field, $instance) { |
|
|
// TODO |
|
|
return NULL; |
|
|
} |
|
|
|
|
| 412 |
/********************************************************************** |
/********************************************************************** |
| 413 |
* Field Type API: Formatters |
* Field Type API: Formatters |
| 414 |
**********************************************************************/ |
**********************************************************************/ |
| 440 |
* Theme function for 'default' text field formatter. |
* Theme function for 'default' text field formatter. |
| 441 |
*/ |
*/ |
| 442 |
function theme_field_formatter_combo_default($element) { |
function theme_field_formatter_combo_default($element) { |
| 443 |
return 'combo field'; |
return '<pre>'.print_r($element,1).'</pre>'; |
| 444 |
} |
} |
| 445 |
|
|
| 446 |
/********************************************************************** |
/********************************************************************** |
| 469 |
* 'rgb' form element. |
* 'rgb' form element. |
| 470 |
*/ |
*/ |
| 471 |
function combo_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0) { |
function combo_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0) { |
| 472 |
return array(); |
$form[$field['field_name']] = array( |
| 473 |
|
'#type' => 'fieldset', |
| 474 |
|
'#title' => $instance['label'], |
| 475 |
|
'#description' => 'Combofield widget!', |
| 476 |
|
'#tree' => TRUE, |
| 477 |
|
); |
| 478 |
|
|
| 479 |
|
$delta = 0; |
| 480 |
|
foreach ($items as $delta => $item) { |
| 481 |
|
// Create the combo entity for $field/$combo_delta on $object. |
| 482 |
|
// WTF is $object? The object this form is being created to edit. |
| 483 |
|
// Do I get that info in $form_state? If not, I can/should store |
| 484 |
|
// it in the combo field's columns, so it will be included in $item. |
| 485 |
|
// TODO: Pass $item so $combo_entity gets default values (?) |
| 486 |
|
$combo_entity = _combo_create_entity($obj_type, $object, $field, $delta); |
| 487 |
|
dpm($combo_entity); |
| 488 |
|
field_attach_form('combo', $combo_entity, $form[$field['field_name']][$langcode][$delta], $form_state, $langcode); |
| 489 |
|
} |
| 490 |
|
$combo_entity = _combo_create_entity(NULL, NULL, $field, $delta); |
| 491 |
|
field_attach_form('combo', $combo_entity, $form[$field['field_name']][$langcode][$delta], $form_state, $langcode); |
| 492 |
|
// TODO: Problem. Widgets assume their field and instance are |
| 493 |
|
// accessible via $form['#fields'][field_name]. I can copy them |
| 494 |
|
// there now, but this makes me realize that it is possible to |
| 495 |
|
// attach the same field to an object twice via different combo |
| 496 |
|
// fields, so $form['#fields'][field_name] will no longer be |
| 497 |
|
// unique. Also, something is clearly going to be bite me soon |
| 498 |
|
// regarding widgets looking for their form elements in the wrong |
| 499 |
|
// place... perhaps I am re-inventing logic needed for fieldgroups? |
| 500 |
|
$form['#fields'] = array_merge($form['#fields'], $form[$field['field_name']][$langcode][$delta]['#fields']); |
| 501 |
|
|
| 502 |
|
return $form; |
| 503 |
} |
} |