Projet

Général

Profil

Paste
Télécharger (5,17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_collection / field_collection.pages.inc @ 5e632cae

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides the field collection item view / edit / delete pages.
6
 */
7

    
8
// TODO: fix being embedded in a host with revisions.
9

    
10
/**
11
 * Field Collection item view page.
12
 */
13
function field_collection_item_page_view($field_collection_item) {
14
  // @todo: Set breadcrumb including the host.
15
  drupal_set_title($field_collection_item->label());
16
  return $field_collection_item->view('full', NULL, TRUE);
17
}
18

    
19
/**
20
 * Form for editing a field collection item.
21
 * @todo implement hook_forms().
22
 */
23
function field_collection_item_form($form, &$form_state, $field_collection_item) {
24
  if (!isset($field_collection_item->is_new)) {
25
    drupal_set_title($field_collection_item->label());
26
  }
27
  $form_state += array('field_collection_item' => $field_collection_item);
28

    
29
  // Hack: entity_form_field_validate() needs the bundle to be set.
30
  // @todo: Fix core and remove the hack.
31
  $form['field_name'] = array('#type' => 'value', '#value' => $field_collection_item->field_name);
32

    
33
  $langcode = entity_language('field_collection_item', $field_collection_item);
34
  field_attach_form('field_collection_item', $field_collection_item, $form, $form_state, $langcode);
35

    
36
  $form['actions'] = array('#type' => 'actions', '#weight' => 50);
37
  $form['actions']['submit'] = array(
38
    '#type' => 'submit',
39
    '#value' => t('Save'),
40
    '#weight' => 5,
41
  );
42
  return $form;
43
}
44

    
45
/**
46
 * Validation callback.
47
 */
48
function field_collection_item_form_validate($form, &$form_state) {
49
  entity_form_field_validate('field_collection_item', $form, $form_state);
50
}
51

    
52
/**
53
 * Submit builder. Extracts the form values and updates the entity.
54
 */
55
function field_collection_item_form_submit_build_field_collection($form, $form_state) {
56
  entity_form_submit_build_entity('field_collection_item', $form_state['field_collection_item'], $form, $form_state);
57
  return $form_state['field_collection_item'];
58
}
59

    
60
/**
61
 * Submit callback that permanently saves the changes to the entity.
62
 */
63
function field_collection_item_form_submit($form, &$form_state) {
64
  $field_collection_item = field_collection_item_form_submit_build_field_collection($form, $form_state);
65
  $field_collection_item->save();
66
  drupal_set_message(t('The changes have been saved.'));
67
  $form_state['redirect'] = $field_collection_item->path();
68
}
69

    
70
/**
71
 * Form for deleting a field collection item.
72
 */
73
function field_collection_item_delete_confirm($form, &$form_state, $field_collection_item) {
74
  $form_state += array('field_collection_item' => $field_collection_item);
75
  return confirm_form($form,
76
    t('Are you sure you want to delete %label?', array('%label' => $field_collection_item->label())),
77
    $field_collection_item->path(),
78
    t('This action cannot be undone.'),
79
    t('Delete'),
80
    t('Cancel')
81
  );
82
}
83

    
84
/**
85
 * Submit callback for deleting a field collection item.
86
 */
87
function field_collection_item_delete_confirm_submit($form, &$form_state) {
88
  $field_collection_item = $form_state['field_collection_item'];
89
  entity_delete('field_collection_item', $field_collection_item->item_id);
90
  drupal_set_message(t('%label has been deleted.', array('%label' => drupal_ucfirst($field_collection_item->label()))));
91
  $form_state['redirect'] = '<front>';
92
}
93

    
94
/**
95
 * Add a new field collection item.
96
 *
97
 * @todo: Support optionally passing in the revision_id and langcode parameters.
98
 */
99
function field_collection_item_add($field_name, $entity_type, $entity_id, $revision_id = NULL, $langcode = NULL) {
100
  $info = entity_get_info();
101
  if (!isset($info[$entity_type])) {
102
    return MENU_NOT_FOUND;
103
  }
104
  $result = entity_load($entity_type, array($entity_id));
105
  $entity = reset($result);
106
  if (!$entity) {
107
    return MENU_NOT_FOUND;
108
  }
109
  // Ensure the given entity is of a bundle that has an instance of the field.
110
  list($id, $rev_id, $bundle) = entity_extract_ids($entity_type, $entity);
111
  $instance = field_info_instance($entity_type, $field_name, $bundle);
112
  if (!$instance) {
113
    return MENU_NOT_FOUND;
114
  }
115

    
116
  // Check field cardinality.
117
  $field = field_info_field($field_name);
118
  $langcode = !empty($field['translatable']) ? entity_language($entity_type, $entity) : LANGUAGE_NONE;
119

    
120
  if (!($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || !isset($entity->{$field_name}[$langcode]) || count($entity->{$field_name}[$langcode]) < $field['cardinality'])) {
121
    return MENU_ACCESS_DENIED;
122
  }
123

    
124
  $field_collection_item = entity_create('field_collection_item', array('field_name' => $field_name));
125
  // Do not link the field collection item with the host entity at this point,
126
  // as during the form-workflow we have multiple field collection item entity
127
  // instances, which we don't want link all with the host.
128
  // That way the link is going to be created when the item is saved.
129
  $field_collection_item->setHostEntity($entity_type, $entity, $langcode, FALSE);
130

    
131
  $label = $field_collection_item->translatedInstanceLabel();
132
  $title = $field['cardinality'] == 1 ? $label : t('Add new !instance_label', array('!instance_label' => $label));
133
  drupal_set_title($title);
134

    
135
  // Make sure the current user has access to create a field collection item.
136
  if (!entity_access('create', 'field_collection_item', $field_collection_item)) {
137
    return MENU_ACCESS_DENIED;
138
  }
139
  return drupal_get_form('field_collection_item_form', $field_collection_item);
140
}