field_collection.pages.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @file
  4. * Provides the field collection item view / edit / delete pages.
  5. */
  6. // TODO: fix being embedded in a host with revisions.
  7. /**
  8. * Field collection item view page.
  9. */
  10. function field_collection_item_page_view($field_collection_item) {
  11. // @todo: Set breadcrumb including the host.
  12. drupal_set_title($field_collection_item->label());
  13. return $field_collection_item->view('full', NULL, TRUE);
  14. }
  15. /**
  16. * Form for editing a field collection item.
  17. * @todo implement hook_forms().
  18. */
  19. function field_collection_item_form($form, &$form_state, $field_collection_item) {
  20. if (!isset($field_collection_item->is_new)) {
  21. drupal_set_title($field_collection_item->label());
  22. }
  23. $form_state += array('field_collection_item' => $field_collection_item);
  24. // Hack: entity_form_field_validate() needs the bundle to be set.
  25. // @todo: Fix core and remove the hack.
  26. $form['field_name'] = array('#type' => 'value', '#value' => $field_collection_item->field_name);
  27. field_attach_form('field_collection_item', $field_collection_item, $form, $form_state);
  28. $form['actions'] = array('#type' => 'actions', '#weight' => 50);
  29. $form['actions']['submit'] = array(
  30. '#type' => 'submit',
  31. '#value' => t('Save'),
  32. '#weight' => 5,
  33. );
  34. return $form;
  35. }
  36. /**
  37. * Validation callback.
  38. */
  39. function field_collection_item_form_validate($form, &$form_state) {
  40. entity_form_field_validate('field_collection_item', $form, $form_state);
  41. }
  42. /**
  43. * Submit builder. Extracts the form values and updates the entity.
  44. */
  45. function field_collection_item_form_submit_build_field_collection($form, $form_state) {
  46. entity_form_submit_build_entity('field_collection_item', $form_state['field_collection_item'], $form, $form_state);
  47. return $form_state['field_collection_item'];
  48. }
  49. /**
  50. * Submit callback that permanently saves the changes to the entity.
  51. */
  52. function field_collection_item_form_submit($form, &$form_state) {
  53. $field_collection_item = field_collection_item_form_submit_build_field_collection($form, $form_state);
  54. $field_collection_item->save();
  55. drupal_set_message(t('The changes have been saved.'));
  56. $form_state['redirect'] = $field_collection_item->path();
  57. }
  58. /**
  59. * Form for deleting a field collection item.
  60. */
  61. function field_collection_item_delete_confirm($form, &$form_state, $field_collection_item) {
  62. $form_state += array('field_collection_item' => $field_collection_item);
  63. return confirm_form($form,
  64. t('Are you sure you want to delete %label?', array('%label' => $field_collection_item->label())),
  65. $field_collection_item->path(),
  66. t('This action cannot be undone.'),
  67. t('Delete'),
  68. t('Cancel')
  69. );
  70. }
  71. /**
  72. * Submit callback for deleting a field collection item.
  73. */
  74. function field_collection_item_delete_confirm_submit($form, &$form_state) {
  75. $field_collection_item = $form_state['field_collection_item'];
  76. $field_collection_item->deleteRevision();
  77. drupal_set_message(t('%label has been deleted.', array('%label' => drupal_ucfirst($field_collection_item->label()))));
  78. $form_state['redirect'] = '<front>';
  79. }
  80. /**
  81. * Add a new field collection item.
  82. *
  83. * @todo: Support optionally passing in the revision_id and langcode parameters.
  84. */
  85. function field_collection_item_add($field_name, $entity_type, $entity_id, $revision_id = NULL, $langcode = NULL) {
  86. $info = entity_get_info();
  87. if (!isset($info[$entity_type])) {
  88. return MENU_NOT_FOUND;
  89. }
  90. $result = entity_load($entity_type, array($entity_id));
  91. $entity = reset($result);
  92. if (!$entity) {
  93. return MENU_NOT_FOUND;
  94. }
  95. // Ensure the given entity is of a bundle that has an instance of the field.
  96. list($id, $rev_id, $bundle) = entity_extract_ids($entity_type, $entity);
  97. $instance = field_info_instance($entity_type, $field_name, $bundle);
  98. if (!$instance) {
  99. return MENU_NOT_FOUND;
  100. }
  101. // Check field cardinality.
  102. $field = field_info_field($field_name);
  103. $langcode = LANGUAGE_NONE;
  104. if (!($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || !isset($entity->{$field_name}[$langcode]) || count($entity->{$field_name}[$langcode]) < $field['cardinality'])) {
  105. drupal_set_message(t('Too many items.'), 'error');
  106. return '';
  107. }
  108. $field_collection_item = entity_create('field_collection_item', array('field_name' => $field_name));
  109. // Do not link the field collection item with the host entity at this point,
  110. // as during the form-workflow we have multiple field collection item entity
  111. // instances, which we don't want link all with the host.
  112. // That way the link is going to be created when the item is saved.
  113. $field_collection_item->setHostEntity($entity_type, $entity, LANGUAGE_NONE, FALSE);
  114. $label = $field_collection_item->translatedInstanceLabel();
  115. $title = ($field['cardinality'] == 1) ? $label : t('Add new !instance_label', array('!instance_label' => $label));
  116. drupal_set_title($title);
  117. // Make sure the current user has access to create a field collection item.
  118. if (!field_collection_item_access('create', $field_collection_item)) {
  119. return MENU_ACCESS_DENIED;
  120. }
  121. return drupal_get_form('field_collection_item_form', $field_collection_item);
  122. }