editableviews_plugin_style_row_edit_table.inc

  1. nittany7 modules/contrib/editableviews/editableviews_plugin_style_row_edit_table.inc
  2. cis7 modules/contrib/editableviews/editableviews_plugin_style_row_edit_table.inc

Classes

Namesort descending Description
editableviews_plugin_style_row_edit_table Plugin class for the Editable Table style.
editableviews_style_helper Helper class for the style plugin.

File

modules/contrib/editableviews/editableviews_plugin_style_row_edit_table.inc
View source
  1. <?php
  2. /**
  3. * Plugin class for the Editable Table style.
  4. */
  5. class editableviews_plugin_style_row_edit_table extends views_plugin_style_table {
  6. /**
  7. * Initialize a style plugin.
  8. */
  9. function init(&$view, &$display, $options = NULL) {
  10. parent::init($view, $display, $options);
  11. // Get the helper object. This abstracts out a number of things we do here,
  12. // in order that other style plugins can use them too.
  13. $this->helper = new editableviews_style_helper($this);
  14. }
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. // Todo: this should technically be on the helper, but it's faffy as it
  18. // is apparently not always there, if Views code with the same
  19. // pattern is anything to go by!
  20. $options['relationship_creation_bundle'] = array('default' => array());
  21. $options['save_messages'] = array('default' => 'individual');
  22. return $options;
  23. }
  24. /**
  25. * The options form for the given style.
  26. */
  27. function options_form(&$form, &$form_state) {
  28. parent::options_form($form, $form_state);
  29. // Everything we add to the options form is common and thus in the helper.
  30. $this->helper->options_form($form, $form_state);
  31. }
  32. function validate() {
  33. $errors = parent::validate();
  34. $relationship_handlers = $this->display->handler->get_handlers('relationship');
  35. $field_handlers_grouped = $this->helper->get_editable_field_handlers_grouped();
  36. //dsm($field_handlers_grouped);
  37. foreach ($relationship_handlers as $relationship_id => $relationship_handler) {
  38. // We don't care about required relationships.
  39. if ($relationship_handler->options['required']) {
  40. continue;
  41. }
  42. //dsm($relationship_handler);
  43. // We don't care if there are no editable fields on the relationship.
  44. if (empty($field_handlers_grouped[$relationship_id])) {
  45. continue;
  46. }
  47. if (!isset($relationship_handler->definition['editableviews_direction'])) {
  48. $errors[] = t("The relationship '@relationship' is not compatible with editable fields that may cause creation of entities for empty data. The relationship should be set to be 'required'.", array(
  49. '@relationship' => $relationship_handler->options['label']
  50. ));
  51. }
  52. if (!isset($this->options['relationship_creation_bundle'][$relationship_id])) {
  53. $errors[] = t("Display @display is set to use a editable fields and the '@relationship' relationship is not set to be required: the bundle of entities to create on this relationship must be set in the Editable Table options.", array(
  54. '@display' => $this->display->display_title,
  55. '@relationship' => $relationship_handler->options['label']
  56. ));
  57. }
  58. }
  59. return $errors;
  60. }
  61. /**
  62. * Add anything to the query that we might need to.
  63. */
  64. function query() {
  65. parent::query();
  66. // Everything we do to the query is common and thus in the helper.
  67. $this->helper->query();
  68. }
  69. /**
  70. * Helper to get the handlers for editable fields.
  71. *
  72. * @return
  73. * An array of field handlers, in the same format as get_handlers() returns,
  74. * but containing only those handlers which are for editable fields.
  75. */
  76. function get_edit_field_handlers() {
  77. $handlers = $this->display->handler->get_handlers('field');
  78. $edit_field_handlers = array();
  79. foreach ($handlers as $key => $handler) {
  80. if (!empty($handler->editable)) {
  81. $edit_field_handlers[$key] = $handler;
  82. }
  83. }
  84. return $edit_field_handlers;
  85. }
  86. /**
  87. * Render all of the fields for a given style and store them on the object.
  88. *
  89. * @param $result
  90. * The result array from $view->result
  91. */
  92. function render_fields($result) {
  93. if (!$this->uses_fields()) {
  94. return;
  95. }
  96. if (!isset($this->rendered_fields)) {
  97. parent::render_fields($result);
  98. $this->insert_form_elements($result);
  99. }
  100. return $this->rendered_fields;
  101. }
  102. /**
  103. * Insert the field form elements into the rendered View fields.
  104. *
  105. * @param $result
  106. * The result array from $view->result
  107. */
  108. function insert_form_elements($result) {
  109. //dsm($result, '$result');
  110. // Get our edit field handlers.
  111. $edit_field_handlers = $this->get_edit_field_handlers();
  112. //dsm($edit_field_handlers, '$edit_field_handlers');
  113. $relationship_handlers = $this->display->handler->get_handlers('relationship');
  114. //dsm($this->view->relationship);
  115. // Build an array of the field names to make editable.
  116. // The keys are the id keys of the Views handlers.
  117. // For non-field-API fields, the definition must include this property.
  118. $edit_fields = array();
  119. // Create the keys in this so they exist even for relationships with no
  120. // editable fields.
  121. $edit_field_handlers_grouped = array_fill_keys(array_keys($relationship_handlers), array());
  122. $edit_field_handlers_grouped[$this->view->base_table] = array();
  123. foreach ($edit_field_handlers as $handler_id => $handler) {
  124. //dsm($handler, "field handler $handler_id");
  125. $edit_fields[$handler_id] = $handler->definition['field_name'];
  126. // Build an array of handlers grouped by relationship ID.
  127. // This is for the form builder to only work on the handlers that are
  128. // relevant to the entity's relationship.
  129. $field_handler_relationship_id = $handler->options['relationship'];
  130. if ($field_handler_relationship_id == 'none') {
  131. $field_handler_relationship_id = $this->view->base_table;
  132. }
  133. $edit_field_handlers_grouped[$field_handler_relationship_id][$handler_id] = $handler;
  134. }
  135. // Build an array of entities that we should be working with. We load the
  136. // that are implicit in the view result, creating new ones if there are any
  137. // gaps.
  138. // The entity ID fields have been added to the view result by query().
  139. $result_entities = array();
  140. foreach ($result as $index => $result_row) {
  141. foreach ($this->helper->relationship_entity_fields as $relationship_id => $relationship_entity_data) {
  142. $entity_type = $relationship_entity_data['entity_type'];
  143. // Get the entity ID out of the result.
  144. $entity_id = $result_row->{$relationship_entity_data['id_field_alias']};
  145. // Get the entities we work with, and build an array of them.
  146. if (isset($entity_id)) {
  147. $entity = entity_load_single($entity_type, $entity_id);
  148. $result_entities[$entity_type][$entity_id] = $entity;
  149. }
  150. else {
  151. if (count($edit_field_handlers_grouped[$relationship_id])) {
  152. // If there is no entity, and the relationship has editable fields,
  153. // we create one (i.e., make the object without saving it). We give
  154. // this a fake entity id, composed of the relationship handler id
  155. // and the index so it's unique.
  156. $entity_id = $relationship_id . ':' . $index;
  157. $entity = $this->helper->entity_create($relationship_id);
  158. $result_entities[$entity_type][$entity_id] = $entity;
  159. }
  160. }
  161. // Build a lookup from entity type and entity to the result row index
  162. // and relationship. It helps to conceptualize this as giving us
  163. // coordinates for where the entity has fields in the view table:
  164. // the index gives the row, and the relationship gives the column(s).
  165. // This is for the form builder to be able to get to the right result
  166. // row and to know which handlers to get form elements from.
  167. $result_indexes[$entity_type][$entity_id] = array($relationship_id, $index);
  168. // Build a lookup array of the same coordinates, but towards the entity:
  169. // keys are relationship ID then index, values are entity type and entity.
  170. $result_indexes_reverse[$relationship_id][$index] = array($entity_type, $entity_id);
  171. }
  172. }
  173. //dsm($result_entities, '$result_entities');
  174. //dsm($result_indexes_reverse, '$result_indexes_reverse');
  175. // Make a combined array of coordinate lookups, both forward and reverse.
  176. // TODO: eventually replace everything to work with this arrays instead of
  177. // the two separate ones.
  178. $results_coordinates = array(
  179. 'entities_to_results' => $result_indexes,
  180. 'results_to_entities' => $result_indexes_reverse,
  181. );
  182. // Build up some lookups pertaining to field handlers, and set the editable
  183. // fields on new entities.
  184. $result_entity_ids = array();
  185. foreach ($result as $index => $result_row) {
  186. foreach ($edit_field_handlers as $handler_id => $handler) {
  187. // Get the entity type and entity for this field handler from the
  188. // relationship lookup.
  189. $field_handler_relationship_id = $handler->options['relationship'];
  190. if ($field_handler_relationship_id == 'none') {
  191. $field_handler_relationship_id = $this->view->base_table;
  192. }
  193. // Add the field_name for this field handler to the list on the entity
  194. // which keeps track of them.
  195. list($entity_type, $entity_id) = $result_indexes_reverse[$field_handler_relationship_id][$index];
  196. if (!is_numeric($entity_id)) {
  197. $entity = $result_entities[$entity_type][$entity_id];
  198. $entity->editableviews_exposed_fields[] = $handler->definition['field_name'];
  199. }
  200. // Build a lookup array from index and field handler to the entity type
  201. // and entity. This is to get to the right form element to include when
  202. // we finally render our fields.
  203. // Just get the entity coordinates from the relationship lookup.
  204. $result_entity_ids[$index][$handler_id] = $result_indexes_reverse[$field_handler_relationship_id][$index];
  205. }
  206. }
  207. //dsm($result_entity_ids, '$result_entity_ids');
  208. // Now we have built up all our entities, go over them again and add
  209. // the connecting properties to any new ones.
  210. // In other words:
  211. // - On a forward relationship, the existing entity on the relationship's
  212. // base needs to point to the new entity that is (potentially) about to
  213. // be saved.
  214. // - On a reverse relationship, the new entity that is about to be created
  215. // needs to point back to the existing entity on the relationship's base.
  216. // Here we figure out the id we need to point to, and the property to point
  217. // to it in.
  218. $this->helper->connect_new_entities($result_entities, $results_coordinates, $edit_field_handlers_grouped);
  219. //dsm($result_entities, '$result_entities post connect');
  220. // Load up the form render array.
  221. $this->get_form($result_entities, $results_coordinates, $edit_field_handlers_grouped);
  222. // Doctor the view's rendered fields to add in the form elements for
  223. // the appropriate entity and field.
  224. foreach ($this->rendered_fields as $index => $rendered_fields) {
  225. foreach ($edit_fields as $handler_id => $field_name) {
  226. // Get the already rendered field.
  227. $rendered_field = $this->rendered_fields[$index][$handler_id];
  228. // Get the entity type and entity that this field handler shows from our
  229. // lookup array, so that we can pick out the form element to render
  230. // for it.
  231. list($entity_type, $entity_id) = $result_entity_ids[$index][$handler_id];
  232. // TODO! theme this!!
  233. $this->rendered_fields[$index][$handler_id] = '<div class="views-row-edit-static">' . $rendered_field . '</div>';
  234. $this->rendered_fields[$index][$handler_id] .= '<div class="views-row-edit-edit">' . drupal_render($this->form[$entity_type][$entity_id][$field_name]) . '</div>';
  235. }
  236. }
  237. }
  238. /**
  239. * Helper method. Retrieves the form render array.
  240. *
  241. * @param $entities
  242. * An array of entities to get the form for, keyed first by entity type and
  243. * then by entity id.
  244. * @param $results_coordinates
  245. * An array containing coordinates lookups for getting entity type and entity
  246. * from row id and relationship, and the same the other way round. Contains:
  247. * - 'entities_to_results': A nested array keyed by entity type then entity
  248. * id. The final values are flat arrays of the relationship id and result
  249. * index.
  250. * - 'results_to_entities': A nested array keyed by relationship id then
  251. * result index. The final values are flat arrays of the entity type and
  252. * entity id.
  253. * @param $edit_field_handlers
  254. * An array of field handlers to provide form elements for, grouped by
  255. * their relationship.
  256. * See editableviews_entity_form() for details.
  257. */
  258. function get_form($entities, $results_coordinates, $edit_field_handlers) {
  259. // Create a dynamic form ID using the base name (for quick recognition
  260. // in hook_forms()) and the view name. This allows hook_form_alter() to
  261. // target forms for specific views. We don't add the display name as there's
  262. // no clean way to mark the separation between that and the view name.
  263. // @see editableviews_forms()
  264. $form_id = 'editableviews_entity_form' . '_' . $this->view->name;
  265. // We store this rather than return it, as it's used in different places.
  266. $this->form = drupal_get_form($form_id, $entities, $results_coordinates, $edit_field_handlers, $this->view);
  267. }
  268. /**
  269. * Render the display in this style.
  270. */
  271. function render() {
  272. // Get the rendered view output.
  273. $view_render = parent::render();
  274. // Stick it INSIDE the form as plain markup, so that the HTML FORM element
  275. // goes around everything.
  276. $this->form['view'] = array(
  277. '#markup' => $view_render,
  278. );
  279. return $this->form;
  280. }
  281. }
  282. /**
  283. * Helper class for the style plugin.
  284. *
  285. * This abstracts out a number of things the style plugin needs to do, in order
  286. * that other style plugins can use them too.
  287. */
  288. class editableviews_style_helper {
  289. /**
  290. * A lookup from relationships to the data needed to load their entities.
  291. *
  292. * This is an array of the relationships on the view which correspond to
  293. * entity base tables, including the base table (as a pseudo-relationship).
  294. * The key is the relationship id (in the case of the base table, this is just
  295. * the table name itself). Each value is an array containing:
  296. * - 'entity_type': The entity type this relationship brings to the view.
  297. * - 'id_field_alias': The field alias of the field on the view in which to
  298. * find the entity's ID. This field is ensured by our query().
  299. */
  300. public $relationship_entity_fields = array();
  301. function __construct(&$plugin) {
  302. $this->plugin = &$plugin;
  303. }
  304. /**
  305. * Provide common options for editable style plugins.
  306. */
  307. function options_form(&$form, &$form_state) {
  308. // Add a fieldset to allow setting of a creation bundle for all the
  309. // relationships that are non-required. This is because a non-required
  310. // relationship may cause empty data in the result, and if this has editable
  311. // fields, then entering data into those field's form elements causes the
  312. // creation of a new entity. Which we need a bundle for.
  313. $relationship_handlers = $this->plugin->display->handler->get_handlers('relationship');
  314. // Get our edit field handlers.
  315. $edit_field_handlers = $this->plugin->get_edit_field_handlers();
  316. // Collect the relationships these are on.
  317. $edit_relationship_handlers = array();
  318. foreach ($edit_field_handlers as $field_handler_id => $field_handler) {
  319. // Because we're not in the process of querying, the relationship is only
  320. // set in the options.
  321. $relationship_id = $field_handler->options['relationship'];
  322. // Skip edit field handlers that are on the base.
  323. if ($relationship_id == 'none') {
  324. continue;
  325. }
  326. // Argh, do we need to contend with the alias of the relationship here??
  327. // Skip a relationship that is required: these will never provide an empty
  328. // row, and so never require entity creation.
  329. if ($relationship_handlers[$relationship_id]->options['required']) {
  330. continue;
  331. }
  332. // If we're still here, this is a relationship we need to consider.
  333. $edit_relationship_handlers[$relationship_id] = $relationship_handlers[$relationship_id];
  334. }
  335. // Only show this fieldset if there are relationships to consider.
  336. if (count($edit_relationship_handlers)) {
  337. $form['relationship_creation_bundle'] = array(
  338. '#type' => 'fieldset',
  339. '#title' => t('Relationship entity creation bundles'),
  340. '#description' => t('A relationship which is set to not required may produce empty form elements, which thus require the creation of a new entity if populated with data. The bundle for these new entities must be specified here.'),
  341. '#weight' => -1,
  342. );
  343. foreach ($edit_relationship_handlers as $relationship_id => $relationship_handler) {
  344. // The 'base' of a relationship is the table it brings.
  345. $table = $relationship_handler->definition['base'];
  346. // Get the entity type from the table.
  347. $table_data = views_fetch_data($table);
  348. $entity_type = $table_data['table']['entity type'];
  349. $entity_info = entity_get_info($entity_type);
  350. $options = array();
  351. foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
  352. $options[$bundle] = $bundle_info['label'];
  353. }
  354. $form['relationship_creation_bundle'][$relationship_id] = array(
  355. '#type' => 'select',
  356. '#title' => t('Bundle for new entities created on %label', array(
  357. '%label' => $relationship_handler->options['label'],
  358. )),
  359. '#description' => t('Select the %entity entity bundle for entities created on this relationship.', array(
  360. '%entity' => $entity_info['label'],
  361. )),
  362. '#options' => $options,
  363. '#required' => TRUE,
  364. );
  365. // We have to check the default value, as the key in the array is variable
  366. // because it's the relationship handler ID. That means that Views won't
  367. // have it set in option_definition().
  368. if (isset($this->plugin->options['relationship_creation_bundle'][$relationship_id])) {
  369. $form['relationship_creation_bundle'][$relationship_id]['#default_value'] = $this->plugin->options['relationship_creation_bundle'][$relationship_id];
  370. }
  371. }
  372. }
  373. $form['save_messages'] = array(
  374. '#type' => 'select',
  375. '#title' => t("Save messages"),
  376. '#description' => t('The messages to show the user when the view form is saved.'),
  377. '#options' => array(
  378. 'none' => t('Show no messages'),
  379. 'summary' => t('Show a single message summarizing the changes'),
  380. 'individual' => t('Show a message for each saved entity'),
  381. ),
  382. '#default_value' => $this->plugin->options['save_messages'],
  383. );
  384. }
  385. /**
  386. * Add anything to the query that we might need to.
  387. *
  388. * For the base and each relationship that brings an entity table, add the
  389. * entity ID field for that entity. This ensures that we can load the entities
  390. * when we need to get form elements for them.
  391. *
  392. * We do this for all relationships, not just those which have editable fields
  393. * on them, because we may need access to entities that editable field
  394. * entities need to point to when creating entities.
  395. */
  396. function query() {
  397. // For each relationship that provides an entity table (including the base
  398. // pseudo-relationship), add the field for that entity's ID.
  399. $base_table = $this->plugin->view->base_table;
  400. // Do the View base first.
  401. $table_data = views_fetch_data($this->plugin->view->base_table);
  402. if (isset($table_data['table']['entity type'])) {
  403. // We don't need to ensure this field is on the query: the id field on
  404. // the base table always is.
  405. $this->relationship_entity_fields[$base_table] = array(
  406. 'entity_type' => $table_data['table']['entity type'],
  407. // We don't need to find an alias for a field on the base.
  408. 'id_field_alias' => $table_data['table']['base']['field'],
  409. );
  410. }
  411. // Now the relationships.
  412. $relationship_handlers = $this->plugin->display->handler->get_handlers('relationship');
  413. foreach ($relationship_handlers as $relationship_id => $relationship_handler) {
  414. //dsm($relationship_handler, $relationship_id);
  415. // The 'base' of a relationship is the table it brings.
  416. $table = $relationship_handler->definition['base'];
  417. // Get the entity type from the table.
  418. $table_data = views_fetch_data($table);
  419. if (!isset($table_data['table']['entity type'])) {
  420. // Not an entity base table relationship: skip it.
  421. continue;
  422. }
  423. // Get the entity type and the views field that corresponds to the entity
  424. // id from the table definition.
  425. //dsm($table_data['table'], 'table data');
  426. $entity_type = $table_data['table']['entity type'];
  427. $entity_id_field = $table_data['table']['base']['field'];
  428. // Force the 're
  429. if ($relationship_handler->options['relationship'] == 'none') {
  430. $relationship_relationship = $base_table;
  431. }
  432. else {
  433. $relationship_relationship = $relationship_handler->options['relationship'];
  434. }
  435. //dsm($relationship_relationship, '$relationship_relationship');
  436. // We want the alias for the table the relationship brings, not the table
  437. // it sits on.
  438. $table_alias = $relationship_handler->alias;
  439. //dsm("$relationship_id brings $entity_type, $entity_id_field.\ntable alias is $table_alias");
  440. $entity_id_field_alias = $this->plugin->view->query->add_field($table_alias, $entity_id_field);
  441. $this->relationship_entity_fields[$relationship_id] = array(
  442. 'entity_type' => $entity_type,
  443. 'id_field_alias' => $entity_id_field_alias,
  444. );
  445. }
  446. //dsm($this->relationship_entity_fields);
  447. }
  448. /**
  449. * Returns a new (unsaved) entity for the given relationship ID.
  450. *
  451. * This is needed when editable field handlers are on a non-required
  452. * relationship, and a particular result row has no data there. We create a
  453. * new entity for FieldAPI to work on, and potentially save it on submission
  454. * if the user enters data.
  455. *
  456. * @param $relationship_id
  457. * The id of the relationship that requires a new entity.
  458. *
  459. * @return
  460. * A new, unsaved entity. The entity type is implied by the handler, and
  461. * should be known by the caller. The bundle will be set on this, given by
  462. * the style plugin's options.
  463. */
  464. function entity_create($relationship_id) {
  465. $entity_type = $this->relationship_entity_fields[$relationship_id]['entity_type'];
  466. // This is complex. We know the entity type, but we need to be told
  467. // the bundle: that's one for the plugin settings.
  468. // Then when it's created, we need to know how to set the relationship
  469. // field.
  470. $entity_info = entity_get_info($entity_type);
  471. // Assume this exists, as it must do if the entity is fieldable, and
  472. // if your entity is not fieldable, what are you doing here? ;)
  473. $bundle_key = $entity_info['entity keys']['bundle'];
  474. $values = array(
  475. // The bundle of the new entity is set in the options for this
  476. // style plugin. This has to be set by the user, because there is
  477. // absolutely no other way to sniff this out!
  478. // TODO: cloud cuckoo land, but a form element field to specify
  479. // the bundle for each row would be nice!
  480. $bundle_key => $this->plugin->options['relationship_creation_bundle'][$relationship_id],
  481. );
  482. // Just a little bit of sugar to save this having to be done in a custom
  483. // form submit handler: for nodes and comments, set the uid property
  484. // to the current user. We would do this with anything that has a uid
  485. // property, but entity_get_property_info() calls it 'author' and it's just
  486. // starting to get faffy now.
  487. if ($entity_type == 'node' || $entity_type == 'comment') {
  488. $values['uid'] = $GLOBALS['user']->uid;
  489. }
  490. $entity = entity_create($entity_type, $values);
  491. // Add our own property to the entity, where we keep track of the properties
  492. // that are exposed as form elements in the view. This is how we will
  493. // determine whether or not to save it when the form is submitted.
  494. $entity->editableviews_exposed_fields = array();
  495. // Add our own property to specify whether this needs to be saved or not.
  496. // @see editableviews_entity_form_submit_build_values()
  497. $entity->editableviews_needs_save = FALSE;
  498. return $entity;
  499. }
  500. /**
  501. * Sets the properties so that new entities connect to existing ones.
  502. *
  503. * For a forward relationship, the existing entity must know it has to point
  504. * to the new entity once it has been saved.
  505. * For a reverse relationship, the new entity must have the right property set
  506. * (e.g. an entityreference field) so that it point back to the existing
  507. * entity.
  508. *
  509. * @param $result_entities
  510. * The array of result entities from the style plugin. Passed by reference
  511. * so the entities can be altered. (TODO: is this actually needed??)
  512. * @param $results_coordinates
  513. * The combined coordinates array, containing both the forward and reverse
  514. * lookups for entities and results.
  515. * @see editableviews_plugin_style_row_edit_table::get_form() for details.
  516. * @param $edit_field_handlers_grouped
  517. * The edit field handlers, grouped by relationship handler ID.
  518. */
  519. function connect_new_entities(&$result_entities, $results_coordinates, $edit_field_handlers_grouped) {
  520. $relationship_handlers = $this->plugin->display->handler->get_handlers('relationship');
  521. //dsm($edit_field_handlers_grouped);
  522. //dsm($result_entities);
  523. foreach (array_keys($result_entities) as $entity_type) {
  524. foreach ($result_entities[$entity_type] as $entity_id => $entity) {
  525. // New entities have a non-numeric fake id we just gave them.
  526. if (!is_numeric($entity_id)) {
  527. // Get the views coordinates for this entity.
  528. list($relationship_id, $index) = $results_coordinates['entities_to_results'][$entity_type][$entity_id];
  529. $relationship_handler = $relationship_handlers[$relationship_id];
  530. //dsm($relationship_handler);
  531. // Get the relationship that the relationship is on, so we can then
  532. // get the entity for that relationship.
  533. if (isset($relationship_handler->relationship)) {
  534. $relationship_relationship = $relationship_handler->relationship;
  535. }
  536. else {
  537. $relationship_relationship = $this->plugin->view->base_table;
  538. }
  539. // Only act if the new entity's relationship has editable fields:
  540. // otherwise it's just an empty bunch of table cells, and there's
  541. // nothing to connect to or from.
  542. if (count($edit_field_handlers_grouped[$relationship_relationship]) == 0) {
  543. continue;
  544. }
  545. if ($relationship_handler->definition['editableviews_direction'] == 'forward') {
  546. // Get the entity on our relationship's relationship -- same
  547. // as for reverse.
  548. // Get the entity out of the imaginary Views grid that is on the same
  549. // row as us, and in the $relationship_relationship relationship...
  550. list($referring_entity_type, $referring_entity_id) = $results_coordinates['results_to_entities'][$relationship_relationship][$index];
  551. $referring_entity = $result_entities[$referring_entity_type][$referring_entity_id];
  552. // Store this entity's details on the current, new entity, so that
  553. // when (and if!) we save it, we can go and make the referring
  554. // entity point to it.
  555. $entity->editableviews_future_reference = array(
  556. 'entity_type' => $referring_entity_type,
  557. 'entity_id' => $referring_entity_id,
  558. 'field_name' => $relationship_handler->definition['field_name'],
  559. );
  560. }
  561. else {
  562. // Would be nice to factor this out to a helper method, say
  563. // '$this->new_entity_set_reverse_connection()' but we'd need to
  564. // pass so many variables it's probably just as faffy.
  565. // Get the entity out of the imaginary Views grid that is on the same
  566. // row as us, and in the $relationship_relationship relationship...
  567. list($referred_entity_type, $referred_entity_id) = $results_coordinates['results_to_entities'][$relationship_relationship][$index];
  568. $referred_entity = $result_entities[$referred_entity_type][$referred_entity_id];
  569. // From here on, this is just reverse relationships!
  570. $wrapper = entity_metadata_wrapper($entity_type, $entity);
  571. // This is what we need to set on the new entity in a reverse relationship.
  572. $relationship_field_name = $relationship_handler->definition['field_name'];
  573. // Make the new entity point to the entity on its relationship's
  574. // relationship.
  575. $wrapper->{$relationship_field_name}->set($referred_entity_id);
  576. }
  577. }
  578. }
  579. }
  580. }
  581. /**
  582. * Get editable field handlers grouped by relationship id.
  583. */
  584. function get_editable_field_handlers_grouped() {
  585. $editable_field_handlers = $this->plugin->get_edit_field_handlers();
  586. $editable_field_handlers_grouped = array();
  587. foreach ($editable_field_handlers as $field_handler_id => $field_handler) {
  588. //dsm($field_handler, '$field_handler');
  589. $relationship_id = $field_handler->options['relationship'];
  590. if ($relationship_id == 'none') {
  591. // TODO: tidy up this WTF!
  592. $relationship_id = 'base';
  593. }
  594. $editable_field_handlers_grouped[$relationship_id][$field_handler_id] = $field_handler;
  595. }
  596. return $editable_field_handlers_grouped;
  597. }
  598. }