Project

General

Profile

Paste
Download (7.81 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / ds / modules / ds_forms / ds_forms.module @ 6e9292aa

1
<?php
2

    
3
/**
4
 * @file
5
 * Display Suite forms integration.
6
 */
7

    
8
/**
9
 * Implements hook_theme().
10
 */
11
function ds_forms_theme() {
12
  $theme_functions = array();
13

    
14
  $theme_functions['ds_forms_custom_form'] = array(
15
    'render element' => 'form',
16
  );
17

    
18
  return $theme_functions;
19
}
20

    
21
/**
22
 * Implements hook_form_FORM_ID_alter().
23
 */
24
function ds_forms_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
25

    
26
  // Determine if this entity type is supported
27
  if (_ds_forms_is_entity_type_supported($form['#entity_type'])) {
28

    
29
    // Add necessary variables for DS Field UI.
30
    $form['#view_mode'] = 'form';
31
    $form_state['no_panels'] = TRUE;
32
    $form_state['no_view_mode_suggestions'] = TRUE;
33

    
34
    // Make sure the refresh works.
35
    if (!module_exists('field_group')) {
36
      // This key is used to store the current updated field.
37
      $form_state += array(
38
        'formatter_settings_edit' => NULL,
39
      );
40
      // Add AJAX wrapper.
41
      $form['fields']['#prefix'] = '<div id="field-display-overview-wrapper">';
42
      $form['fields']['#suffix'] = '</div>';
43

    
44
      // See field_ui.admin.inc for more details on refresh rows.
45
      $form['refresh_rows'] = array('#type' => 'hidden');
46
      $form['refresh'] = array(
47
        '#type' => 'submit',
48
        '#value' => t('Refresh'),
49
        '#op' => 'refresh_table',
50
        '#submit' => array('field_ui_display_overview_multistep_submit'),
51
        '#ajax' => array(
52
          'callback' => 'field_ui_display_overview_multistep_js',
53
          'wrapper' => 'field-display-overview-wrapper',
54
          'effect' => 'fade',
55
          // The button stays hidden, so we hide the AJAX spinner too. Ad-hoc
56
          // spinners will be added manually by the client-side script.
57
          'progress' => 'none',
58
        ),
59
      );
60
      $form['#attached']['css'][] = drupal_get_path('module', 'ds_forms') . '/css/ds_forms.admin.css';
61
    }
62

    
63
    // Attach js.
64
    $form['#attached']['js'][] = drupal_get_path('module', 'ds_forms') . '/js/ds_forms.admin.js';
65

    
66
    // Load Display Suite.
67
    form_load_include($form_state, 'inc', 'ds', 'includes/ds.field_ui');
68
    ds_field_ui_fields_layouts($form, $form_state);
69
  }
70
}
71

    
72
/**
73
 * Implements hook_form_alter().
74
 */
75
function ds_forms_form_alter(&$form, &$form_state, $form_id) {
76
  if ($ds_form = ds_build_load($form, $form_id)) {
77
    if ($layout = ds_get_layout($ds_form->entity_type, $ds_form->bundle, 'form', FALSE)) {
78
      // Add the theming function and add the layout as a class.
79
      $form['#theme'] = array('ds_forms_custom_form');
80
      $class = strtr($layout['layout'], '_', '-');
81
      if ((isset($form['#attributes']['class']) && is_array($form['#attributes']['class'])) || !(isset($form['#attributes']['class']))) {
82
        $form['#attributes']['class'][] = $class;
83
      }
84
      elseif (isset($form['#attributes']['class']) && is_string($form['#attributes']['class'])) {
85
        $form['#attributes']['class'] .= ' ' . $class . ' ';
86
      }
87
    }
88
  }
89
}
90

    
91
/**
92
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
93
 */
94
function ds_forms_field_widget_field_collection_embed_form_alter(&$element, &$form_state, $context){
95
  if ($ds_form = ds_build_load($element, 'field_collection_embed')) {
96
    if ($layout = ds_get_layout($ds_form->entity_type, $ds_form->bundle, 'form', FALSE)) {
97
      // Add the theming function and add the layout as a class.
98
      $element['#theme'] = array('ds_forms_custom_form');
99
      $element['#form_id'] = 'field_collection_embed';
100
      $class = strtr($layout['layout'], '_', '-');
101
      if ((isset($element['#attributes']['class']) && is_array($element['#attributes']['class'])) || !(isset($element['#attributes']['class']))) {
102
        $element['#attributes']['class'][] = $class;
103
      }
104
      elseif (isset($element['#attributes']['class']) && is_string($element['#attributes']['class'])) {
105
        $element['#attributes']['class'] .= ' ' . $class . ' ';
106
      }
107
    }
108
  }
109
}
110
/**
111
 * Helper function to determine if this form can be loaded.
112
 */
113
function ds_build_load($form, $form_id) {
114
  $ds_form = FALSE;
115

    
116
  if (isset($form['#entity_type']) && isset($form['#bundle']) && $form_id != 'field_ui_field_overview_form' && $form_id != 'field_ui_display_overview_form'
117
      && $form_id != 'field_ui_field_settings_form' && $form_id != 'field_ui_widget_type_form' && $form_id != 'field_ui_field_edit_form' && !preg_match('/^editablefields_form_/', $form_id)) {
118
    $ds_form = new stdClass();
119
    $ds_form->entity_type = $form['#entity_type'];
120
    $ds_form->bundle = $form['#bundle'];
121
  }
122

    
123
  return $ds_form;
124
}
125

    
126
/**
127
 * Implements hook_preprocess_ds_forms_custom_form().
128
 */
129
function ds_forms_preprocess_ds_forms_custom_form(&$vars) {
130

    
131
  $form = ds_build_load($vars['form'], $vars['form']['#form_id']);
132
  if (!$form) {
133
    return;
134
  }
135

    
136
  $entity_type = $form->entity_type;
137
  $bundle = $form->bundle;
138

    
139
  if ($layout = ds_get_layout($entity_type, $bundle, 'form', FALSE)) {
140

    
141
    // Theme hook suggestions.
142
    $vars['theme_hook_suggestions'][] = $layout['layout'];
143
    $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $entity_type;
144
    $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $entity_type . '_' . $bundle;
145

    
146
    $form = &$vars['form'];
147

    
148
    // Add path to css file.
149
    if (isset($layout['css'])) {
150
      drupal_add_css($layout['path'] . '/' . $layout['layout'] . '.css');
151
    }
152

    
153
    // Add the hidden region.
154
    $layout['regions']['hidden'] = 'Hidden';
155

    
156
    // Create region variables based on the layout settings.
157
    foreach ($layout['regions'] as $region_name => $region) {
158

    
159
      // Create the region content.
160
      if ($region_name == 'hidden') {
161
        ds_forms_render_region($form, $region_name, $layout);
162
      }
163
      else {
164
        $vars[$region_name] = ds_forms_render_region($form, $region_name, $layout);
165
      }
166

    
167
      // Add extras classes to the region.
168
      $vars[$region_name . '_classes'] = !empty($layout['settings']['classes'][$region_name]) ? ' ' . implode(' ', $layout['settings']['classes'][$region_name]) : '';
169

    
170
      // Add a wrapper to the region.
171
      if (empty($layout['flexible'])) {
172
        $vars[$region_name . '_wrapper'] = isset($layout['settings']['wrappers'][$region_name]) ? $layout['settings']['wrappers'][$region_name] : 'div';
173
      }
174
    }
175

    
176
    // Add layout attributes if any
177
    if (!empty($layout['settings']['layout_attributes'])) {
178
      $vars['layout_attributes'] = ' ' . $layout['settings']['layout_attributes'];
179
    }
180
    else {
181
      $vars['layout_attributes'] = '';
182
    }
183

    
184
    if (isset($layout['settings']['classes']['layout_class'])) {
185
      foreach ($layout['settings']['classes']['layout_class'] as $layout_class) {
186
        $vars['classes_array'][] = $layout_class;
187
      }
188
    }
189

    
190
    // Ensure there is a class
191
    $vars['classes_array'][] = 'ds-form';
192

    
193
    // Merge the classes into a string
194
    $vars['classes'] = implode(' ', $vars['classes_array']);
195

    
196
    // Add a layout wrapper
197
    $vars['layout_wrapper'] = isset($layout['settings']['layout_wrapper']) ? $layout['settings']['layout_wrapper'] : 'div';
198

    
199
    // Add the rest of the form elements
200
    $vars['drupal_render_children'] = drupal_render_children($vars['form']);
201
  }
202
}
203

    
204
/**
205
 * Render a form region.
206
 *
207
 * @param $content
208
 *   An array of content fields.
209
 * @param $region
210
 *   The name of region to render.
211
 * @param $layout
212
 *   The layout definition.
213
 */
214
function ds_forms_render_region(&$content, $region, $layout) {
215
  $output = '';
216

    
217
  if (isset($layout['settings']['regions'][$region])) {
218
    foreach ($layout['settings']['regions'][$region] as $key => $field) {
219
      if ($region == 'hidden') {
220
        $content[$field]['#access'] = FALSE;
221
      }
222
      else {
223
        $output .= drupal_render($content[$field]);
224
      }
225
    }
226
  }
227
  return $output;
228
}
229

    
230
/**
231
 * Determines if this entity type is supported by ds_forms.
232
 *
233
 * Currently supports all fieldable entity types.
234
 */
235
function _ds_forms_is_entity_type_supported($entity_type) {
236
  $info = entity_get_info($entity_type);
237
  return !empty($info['fieldable']);
238
}
239