summaryrefslogtreecommitdiffstats
path: root/breadcrumb2.module
blob: fa3403f4369fecae984cafaada39a5f7913251f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php

/**
 * @file
 * Support for configurable breadcrumbs.
 */
 
/**
 * Implements hook_page_alter().
 */
/* 
function breadcrumb2_page_alter() {
  // See if current page has path breadcrumbs.
  $breadcrumbs = drupal_get_breadcrumb();
  $current_path = current_path();
  $breadcrumb2 = breadcrumb2_load_by_path($current_path);
  if (empty($breadcrumb2)) {
    return;
  }
  // Only keep the first one. 
  if (!empty($breadcrumbs[0])) {
    $breadcrumbs = array($breadcrumbs[0]);
  }
  //$breadcrumb2 = breadcrumb2_load(1);
  $wrapper = entity_metadata_wrapper('breadcrumb2', $breadcrumb2);
  $breadcrumb_links = $wrapper->link->value();
  foreach ($breadcrumb_links as $breadcrumb_link) {
    $breadcrumbs[]= l($breadcrumb_link['title'], $breadcrumb_link['url']);
  }
  //print debug($breadcrumb_links);
  // Set breadcrumbs for current page if it exists.
  if ($breadcrumbs) {
    drupal_set_breadcrumb($breadcrumbs);
  }
}
*/

/**
 * Implement hook_entity_info().
 */
function breadcrumb2_entity_info() {
  $return = array(
    'breadcrumb2' => array(
      'label' => t('Breadcrumb'),
      'plural label' => t('Breadcrumbs'),
      'description' => t('Breadcrumb2 entity breadcrumbs.'),
      'entity class' => 'Breadcrumb',
      'controller class' => 'EntityAPIController',
      'base table' => 'breadcrumb',
      'fieldable' => TRUE,
      'view modes' => array(
        'full' => array(
          'label' => t('Breadcrumb'),
          'custom settings' => FALSE,
        ),
      ),
      'entity keys' => array(
        //'id' => 'pid',
        'id' => 'bid',
      ),
      'bundles' => array(
        'breadcrumb2' => array(
          'label' => t('Breadcrumb'),
          'admin' => array(
            'path' => 'admin/structure/breadcrumbs',
            'access arguments' => array('administer breadcrumbs'),
          ),
        ),
      ),
      'uri callback' => 'entity_class_uri',
      'access callback' => 'breadcrumb2_access',
      'module' => 'breadcrumb2',
      'metadata controller class' => 'Breadcrumb2MetadataController'
    ),
  );
  return $return;
}

/**
 * Implements hook_menu().
 */
function breadcrumb2_menu() {
  $items['admin/structure/breadcrumbs'] = array(
    'title' => 'Breadcrumbs',
    'description' => 'Manage breadcrumbs.',
    'page callback' => 'breadcrumb2_overview_breadcrumbs',
    'access arguments' => array('administer breadcrumbs'),
    'file' => 'breadcrumb2.admin.inc',
  );
  $items['admin/structure/breadcrumbs/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['breadcrumb/add'] = array(
    'title' => 'Add breadcrumb',
    'page callback' => 'breadcrumb2_add',
    'access arguments' => array('administer breadcrumbs'),
    'file' => 'breadcrumb2.admin.inc',
  );
  $items['breadcrumb/%breadcrumb2'] = array(
    'title' => 'Breadcrumb',
    'page callback' => 'breadcrumb2_page_view',
    'page arguments' => array(1),
    'access arguments' => array('administer breadcrumbs'),
    'file' => 'breadcrumb2.admin.inc',
  );
  $items['breadcrumb/%breadcrumb2/view'] = array(
    'title' => 'View',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['breadcrumb/%breadcrumb2/edit'] = array(
    'page callback' => 'breadcrumb2_page_edit',
    'page arguments' => array(1),
    'access arguments' => array('administer breadcrumbs'),
    'weight' => 0,
    'title' => 'Edit',
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'file' => 'breadcrumb2.admin.inc',
  );
  $items['breadcrumb/%breadcrumb2/delete'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('breadcrumb2_delete_confirm_form', 1),
    'access arguments' => array('administer breadcrumbs'),
    'weight' => 1,
    'title' => 'Delete',
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'file' => 'breadcrumb2.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function breadcrumb2_permission() {
  $permissions = array(
    'administer breadcrumbs' => array(
      'title' => t('Administer breadcrumbs'),
      'description' => t('Edit and view all entity breadcrumbs.'),
    ),  
  );
  return $permissions;  
}

/**
 * Determines whether the given user has access to a breadcrumb.
 *
 * @param $op
 *   The operation being performed. One of 'view', 'update', 'create', 'delete'
 *   or just 'edit' (being the same as 'create' or 'update').
 * @param $breadcrumb
 *   Optionally a breadcrumb to check access for. If nothing is
 *   given, access for all breadcrumbs is determined.
 * @param $account
 *   The user to check for. Leave it to NULL to check for the global user.
 * @return boolean
 *   Whether access is allowed or not.
 */
function breadcrumb2_access($op, $breadcrumb = NULL, $account = NULL) {
  if (user_access('administer breadcrumbs', $account)) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Fetch a breadcrumb object. 
 *
 * @param $path
 *   Internal path.
 * @return
 *   A fully-loaded $breadcrumb object or FALSE if it cannot be loaded.
 *
 * @see breadcrumb2_load_multiple()
 */
function breadcrumb2_load_by_path($path) {
  $cache = &drupal_static(__FUNCTION__, array());
  if (!isset($cache[$path])) {
    $breadcrumbs = breadcrumb2_load_multiple(FALSE, array('path' => $path));
    $cache[$path] = reset($breadcrumbs);
    //drupal_set_message('123456');
    return $cache[$path];
  }
  //drupal_set_message('abcd');
  return $cache[$path];
}

/**
 * Fetch a breadcrumb object. 
 *
 * @param $bid
 *   Integer specifying the breadcrumb id.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   A fully-loaded $breadcrumb object or FALSE if it cannot be loaded.
 *
 * @see breadcrumb2_load_multiple()
 */
function breadcrumb2_load($bid, $reset = FALSE) {
  $breadcrumbs = breadcrumb2_load_multiple(array($bid), array(), $reset);
  return reset($breadcrumbs);
}


/**
 * Load multiple breadcrumbs based on certain conditions.
 *
 * @param $bids
 *   An array of breadcrumb IDs.
 * @param $conditions
 *   An array of conditions to match against the {breadcrumb} table.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   An array of breadcrumb objects, indexed by bid.
 *
 * @see entity_load()
 * @see breadcrumb2_load()
 */
function breadcrumb2_load_multiple($bids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('breadcrumb2', $bids, $conditions, $reset);
}

/**
 * Deletes a breadcrumb.
 */
function breadcrumb2_delete(Breadcrumb $breadcrumb) {
  $breadcrumb->delete();
}


/**
 * Delete multiple breadcrumbs.
 *
 * @param $bids
 *   An array of breadcrumb IDs.
 */
function breadcrumb2_delete_multiple(array $bids) {
  entity_get_controller('breadcrumb2')->delete($bids);
}


/**
 * Create a breadcrumb object.
 */
function breadcrumb2_create($values = array()) {
  return new Breadcrumb($values);
}


/**
 * Saves a breadcrumb to the database.
 *
 * @param $breadcrumb
 *   The breadcrumb object.
 */
function breadcrumb2_save(Breadcrumb $breadcrumb) {
  return $breadcrumb->save();
}

/**
 * URI callback for breadcrumbs
 */
function breadcrumb2_uri(Breadcrumb $breadcrumb) {
  return array(
    'path' => 'breadcrumb/' . $breadcrumb->bid,
  );
}

/**
 * The class used for breadcrumb entities
 */
class Breadcrumb extends Entity {
  /**
   * The breadcrumb id.
   *
   * @var integer
   */
  public $bid;

   /**
   * The internal path where breadcrumb should be shown.
   *
   * @var string
   */
  public $path;

  public function __construct($values = array()) {
    parent::__construct($values, 'breadcrumb2');
  }

  protected function defaultLabel() {
    return $this->path;
  }

  protected function defaultUri() {
    return array('path' => 'breadcrumb/' . $this->bid);
  }
  
  public function save() {
    if (empty($this->bid) && (!empty($this->path))) {
      $existing_breadcrumb = breadcrumb2_load_by_path($this->path);
      if (!empty($existing_breadcrumb)) {
         $this->bid = $existing_breadcrumb->bid;
         $this->is_new = FALSE;
      }
    }
    parent::save();
    // Update the static cache from breadcrumb2_load_by_path().
    $cache = &drupal_static('breadcrumb2_load_by_path', array());
    if (isset($cache[$this->path])) {
      $cache[$this->path] = $this;
    }
  }
}

/**
 * Implementation of hook_ctools_plugin_api().
 *
 * Tell Ctools that we support the default_field_validation_rules API.
 */
function breadcrumb2_ctools_plugin_api($owner, $api) {
  if ($owner == 'property_validation' && $api == 'default_property_validation_rules') {
    return array('version' => 2);
  }
}

/**
 * Implements hook_views_api().
 */
function breadcrumb2_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'breadcrumb2') . '/views',
  );
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function breadcrumb2_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  // Add action link to 'breadcrumb/add' on 'admin/structure/breadcrumbs' page.
  if ($root_path == 'admin/structure/breadcrumbs') {
    $item = menu_get_item('breadcrumb/add');
    if ($item['access']) {
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
}

/**
 * Implements hook_admin_paths().
 */
function breadcrumb2_admin_paths() {
  $paths = array(
    'breadcrumb/*' => TRUE,
    'breadcrumb/*/edit' => TRUE,
    'breadcrumb/*/delete' => TRUE,
    'breadcrumb/add' => TRUE,
  );
  return $paths;
}

// To Do: remove include_once. 
//include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'breadcrumb2') . '/views/' . 'breadcrumb2.views.inc';

/**
 * Implements hook_theme_registry_alter().
 */
function breadcrumb2_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['breadcrumb'])) {
    $path = drupal_get_path('module', 'breadcrumb2');
    $theme_registry['breadcrumb']['path'] = $path;
    $theme_registry['breadcrumb']['template'] = 'breadcrumb';
    $theme_registry['breadcrumb']['function'] = NULL;
  }
}

/**
 * Processes variables for breadcrumb.tpl.php.
 *
 * Most themes utilize their own copy of breadcrumb.tpl.php. The default is located
 * inside "breadcrumbs/breadcrumb.tpl.php". Look in there for the full list of
 * variables.
 *
 * The $variables array contains the following arguments:
 * - $breadcrumb
 *
 * @see breadcrumb.tpl.php
 */
function template_preprocess_breadcrumb(&$variables) {
  $variables['classes_array'][] = 'breadcrumb-wrapper';
  // Populate the breadcrumb classes.
  if ($suggestions = theme_get_suggestions(arg(), 'breadcrumb', '-')) {
    foreach ($suggestions as $suggestion) {
      if ($suggestion != 'breadcrumb-front') {
        // Add current suggestion to breadcrumb classes to make it possible to theme
        // the breadcrumb depending on the current page type (e.g. node, admin, user,
        // etc.) as well as more specific data like node-12 or node-edit.
        $variables['classes_array'][] = drupal_html_class($suggestion);
      }
    }
  }
  
  $current_path = current_path();
  $breadcrumb2 = breadcrumb2_load_by_path($current_path);
  if (!empty($breadcrumb2)) {
    $breadcrumbs = array();
    // Only keep the first item. 
    if (!empty($variables['breadcrumb'][0])) {
      $breadcrumbs[] = $variables['breadcrumb'][0];
    }
    $wrapper = entity_metadata_wrapper('breadcrumb2', $breadcrumb2);
    $breadcrumb_links = $wrapper->link->value();
    foreach ($breadcrumb_links as $breadcrumb_link) {
      $breadcrumbs[]= l($breadcrumb_link['title'], $breadcrumb_link['url']);
    }
    $variables['breadcrumb'] = $breadcrumbs;
  }
  
  if (user_access('administer breadcrumbs') && user_access('access contextual links')) {
    $contextual_links_element   =    array(
      '#contextual_links' => array(
        'breadcrumb2' => array('breadcrumb', array('0')),
      )
    ); 

    $contextual_links = array(
      '#type' => 'contextual_links',
      '#contextual_links' => $contextual_links_element['#contextual_links'],
      '#element' => $contextual_links_element,
    );
    $variables['contextual_links'] = drupal_render($contextual_links);
    $variables['classes_array'][] = 'contextual-links-region';
  }

  // Populate the breadcrumb template suggestions.
  if ($suggestions = theme_get_suggestions(arg(), 'breadcrumb')) {
    $variables['theme_hook_suggestions'] = $suggestions;
  }
  
  // Flatten out classes.
  $variables['classes'] = implode(' ', $variables['classes_array']);
}


/**
 * Implements hook_contextual_links_view_alter().
 */
function  breadcrumb2_contextual_links_view_alter(&$element, &$items) {
  if (isset($element['#contextual_links']['breadcrumb2']) && empty($items)) {
    $current_path = current_path();
    $breadcrumb = breadcrumb2_load_by_path($current_path);
    if (!empty($breadcrumb)) {
      $contextual_links_element['#contextual_links']['breadcrumb2'] = array('breadcrumb', array($breadcrumb->bid));
      $element['#links'] = array(
        array('title' => t('Config breadcrumb'), 'href' => 'breadcrumb/' . $breadcrumb->bid . '/edit', "query" => drupal_get_destination()),
      );
    }
    else {
      $element['#links'] = array(
        array('title' => t('Config breadcrumb'), 'href' => 'breadcrumb/add', "query" => array('path' => $current_path, drupal_get_destination())),
      );
    }
  }
}