Projet

Général

Profil

Paste
Télécharger (43,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panelizer / panelizer.install @ 136a805a

1
<?php
2
/**
3
 * @file
4
 * Install, update and uninstall functions for the panelizer module.
5
 */
6

    
7
/**
8
 * Implements hook_schema().
9
 */
10
function panelizer_schema() {
11
  $schema = array();
12

    
13
  // Fields that are shared between the two tables.
14
  $common_fields = array(
15
    'view_mode' => array(
16
      'type' => 'varchar',
17
      'length' => '128',
18
      'description' => 'Contains the view mode this panelizer is for.',
19
      'not null' => TRUE,
20
    ),
21
    'did' => array(
22
      'type' => 'int',
23
      'not null' => TRUE,
24
      'description' => 'The display ID of the panel.',
25
      'no export' => TRUE,
26
    ),
27
    'name' => array(
28
      'type' => 'varchar',
29
      'length' => '255',
30
      'description' => 'The name of the default being used if there is one.',
31
    ),
32
    'css_id' => array(
33
      'type' => 'varchar',
34
      'length' => '255',
35
      'description' => 'The CSS ID this panel should use.',
36
      'default' => '',
37
    ),
38
    'css_class' => array(
39
      'type' => 'varchar',
40
      'length' => '255',
41
      'description' => 'The CSS class this panel should use.',
42
      'default' => '',
43
    ),
44
    'css' => array(
45
      'type' => 'text',
46
      'size' => 'big',
47
      'description' => 'Any CSS the author provided for the panel.',
48
      'object default' => '',
49
    ),
50
    'no_blocks' => array(
51
      'type' => 'int',
52
      'size' => 'tiny',
53
      'description' => 'Whether or not the node disable sidebar blocks.',
54
      'default' => 0,
55
    ),
56
    'title_element' => array(
57
      'type' => 'varchar',
58
      'length' => '255',
59
      'description' => 'The HTML element the title should use.',
60
      'default' => 'H2',
61
    ),
62
    'link_to_entity' => array(
63
      'type' => 'int',
64
      'size' => 'tiny',
65
      'description' => 'Whether or not the title should link to the entity.',
66
      'default' => 1,
67
    ),
68
    'extra' => array(
69
      'type' => 'text',
70
      'size' => 'big',
71
      'description' => 'Contains extra data that can be added by modules.',
72
      'serialize' => TRUE,
73
      'object default' => array(),
74
    ),
75
    'pipeline' => array(
76
      'type' => 'varchar',
77
      'length' => '255',
78
      'description' => 'The render pipeline this panel uses.',
79
      'default' => 'standard',
80
    ),
81
    'contexts' => array(
82
      'type' => 'text',
83
      'size' => 'big',
84
      'description' => 'The contexts configured by the node author.',
85
      'serialize' => TRUE,
86
      'object default' => array(),
87
    ),
88
    'relationships' => array(
89
      'type' => 'text',
90
      'size' => 'big',
91
      'description' => 'The relationship contexts configured by the node author.',
92
      'serialize' => TRUE,
93
      'object default' => array(),
94
    ),
95
  );
96

    
97
  $schema['panelizer_entity'] = array(
98
    'export' => array(
99
      'bulk export' => FALSE,
100
      'can disable' => FALSE,
101
      'identifier' => 'panelizer_node',
102
    ),
103
    'description' => 'Node panelizer references.',
104
    'fields' => array(
105
      'entity_type' => array(
106
        'description' => 'The type of the entity this panel is attached to.',
107
        'type' => 'varchar',
108
        'length' => 128,
109
        'not null' => TRUE,
110
      ),
111
      'entity_id' => array(
112
        'type' => 'int',
113
        'not null' => TRUE,
114
        'default' => 0,
115
        'description' => 'The entity ID this panel is attached to.',
116
      ),
117
      'revision_id' => array(
118
        'description' => 'The revision id of the entity.',
119
        'type' => 'int',
120
        'unsigned' => TRUE,
121
        'not null' => TRUE,
122
      ),
123
    ) + $common_fields,
124
    'primary key' => array('entity_type', 'entity_id', 'revision_id', 'view_mode'),
125
    'indexes' => array(
126
      'revision_id' => array('revision_id'),
127
    ),
128
  );
129

    
130
  $schema['panelizer_defaults'] = array(
131
    'description' => 'Node type panelizer references.',
132
    'export' => array(
133
      'primary key' => 'pnid',
134
      'key' => 'name',
135
      'key name' => 'Name',
136
      'admin_title' => 'title',
137
      'identifier' => 'panelizer',
138
      'default hook' => 'panelizer_defaults',
139
      'api' => array(
140
        'owner' => 'panelizer',
141
        'api' => 'panelizer',
142
        'minimum_version' => 1,
143
        'current_version' => 1,
144
      ),
145
      // 'create callback' => 'panelizer_export_create_callback',
146
      'save callback' => 'panelizer_export_save_callback',
147
      'export callback' => 'panelizer_export_export_callback',
148
      'delete callback' => 'panelizer_export_delete_callback',
149
      'subrecords callback' => 'panelizer_export_delete_callback_subrecords',
150
    ),
151
    'fields' => array(
152
      'pnid' => array(
153
        'type' => 'serial',
154
        'description' => 'The database primary key.',
155
        'no export' => TRUE,
156
        'not null' => TRUE,
157
      ),
158
      'title' => array(
159
        'type' => 'varchar',
160
        'length' => '255',
161
        'description' => 'The human readable title of this default.',
162
      ),
163
      'panelizer_type' => array(
164
        'type' => 'varchar',
165
        'length' => '32',
166
        'description' => 'The panelizer entity type, such as node or user.',
167
      ),
168
      'panelizer_key' => array(
169
        'type' => 'varchar',
170
        'length' => '128',
171
        'description' => 'The panelizer entity bundle.',
172
      ),
173
      'access' => array(
174
        'type' => 'text',
175
        'size' => 'big',
176
        'description' => 'Contains the access control for editing this default.',
177
        'serialize' => TRUE,
178
        'object default' => array(),
179
      ),
180
    ) + $common_fields,
181
    'primary key' => array('pnid'),
182
    'indexes' => array(
183
      'name' => array('name'),
184
      'type_key' => array('panelizer_type', 'panelizer_key'),
185
    ),
186
  );
187

    
188
  return $schema;
189
}
190

    
191
/**
192
 * Implements hook_install().
193
 */
194
function panelizer_install() {
195
  // Set the module weight so it can execute after Panels.
196
  db_update('system')
197
    ->fields(array(
198
      'weight' => 21,
199
    ))
200
    ->condition('name', 'panelizer')
201
    ->execute();
202
}
203

    
204
/**
205
 * Implements hook_uninstall().
206
 */
207
function panelizer_uninstall() {
208
  foreach (entity_get_info() as $entity_type => $entity_info) {
209
    if (isset($entity_info['bundles'])) {
210
      foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
211
        // View mode variables.
212
        if (!empty($entity_info['view modes'])) {
213
          foreach ($entity_info['view modes'] as $view_mode => $view_info) {
214
            variable_del('panelizer_' . $entity_type . ':' . $bundle_name . ':' . $view_mode . '_selection');
215
          }
216
        }
217
        variable_del('panelizer_' . $entity_type . ':' . $bundle_name . ':default_selection');
218
        variable_del('panelizer_' . $entity_type . ':' . $bundle_name . ':page_manager_selection');
219

    
220
        // Other variables.
221
        variable_del('panelizer_defaults_' . $entity_type . '_' . $bundle_name);
222
        variable_del('panelizer_' . $entity_type . ':' . $bundle_name . '_allowed_layouts');
223
        variable_del('panelizer_' . $entity_type . ':' . $bundle_name . '_allowed_layouts_default');
224
        variable_del('panelizer_' . $entity_type . ':' . $bundle_name . '_allowed_types');
225
        variable_del('panelizer_' . $entity_type . ':' . $bundle_name . '_default');
226
      }
227
    }
228
  }
229
}
230

    
231
/**
232
 * Implements hook_requirements().
233
 */
234
function panelizer_requirements($phase) {
235
  $return = array();
236

    
237
  if ($phase === 'runtime') {
238
    // Check for fields displaying on view modes through core config.
239
    $view_modes_to_report = array();
240

    
241
    foreach (entity_get_info() as $entity_type => $entity_info) {
242
      if (isset($entity_info['view modes'])) {
243
        $view_modes = array_keys($entity_info['view modes']);
244
        // Add default to the list of view modes because that might be panelized
245
        // too.
246
        $view_modes[] = 'default';
247

    
248
        if (!empty($entity_info['bundles'])) {
249
          $bundles = $entity_info['bundles'];
250
        }
251
        else {
252
          $bundles = array($entity_type => $entity_type);
253
        }
254

    
255
        foreach ($bundles as $bundle => $bundle_info) {
256
          foreach ($view_modes as $view_mode) {
257
            if (panelizer_view_mode_extra_field_displays($entity_type, $bundle, $view_mode)) {
258
              $view_modes_to_report[] = array(
259
                'entity_type' => $entity_type,
260
                'bundle' => $bundle,
261
                'view_mode' => $view_mode,
262
              );
263
              // @todo Should the display be updated to hide the fields?
264
              // panelizer_hide_fields_on_panelized_view_mode($entity_type, $bundle, $view_mode);
265
            }
266
          }
267
        }
268
      }
269
    }
270

    
271
    if (!empty($view_modes_to_report)) {
272
      $count = count($view_modes_to_report);
273
      $return['double_display'] = array(
274
        'title' => t('Panelizer'),
275
        'value' => t('@number view mode / bundle / entity combination(s) use Panelizer but still have field displays configured.', array('@number' => $count)),
276
        'description' => t('Panelizer replaces the normal entity display. Any view mode that use Panelizer should have all fields hidden through the normal display settings in order to avoid the overhead of generating the output twice.'),
277
        'severity' => REQUIREMENT_WARNING,
278
      );
279
      $description_links = array();
280
      foreach ($view_modes_to_report as $view_mode_array) {
281
        extract($view_mode_array);
282
        $handler = panelizer_entity_plugin_get_handler($entity_type);
283
        $path = $handler->admin_bundle_display_path($bundle, $view_mode);
284
        $description_links[] = array(
285
          'href' => $path,
286
          'title' => implode(':', $view_mode_array),
287
        );
288
      }
289

    
290
      $theme_variables = array(
291
        'links' => $description_links,
292
        'heading' => array(
293
          'text' => 'Entity view modes that should have all fields hidden:',
294
          'level' => 'h6',
295
        ),
296
      );
297
      $return['double_display']['description'] .= theme('links', $theme_variables);
298
    }
299
  }
300
  return $return;
301
}
302

    
303
/**
304
 * Check whether a view mode uses core display of fields and panelizer.
305
 *
306
 * @return bool
307
 *   Whether or not a bundle/view_mode is panelized and has fields displaying
308
 *   through core settings.
309
 */
310
function panelizer_view_mode_extra_field_displays($entity_type, $bundle, $view_mode) {
311
  $handler = panelizer_entity_plugin_get_handler($entity_type);
312

    
313
  if (panelizer_is_panelized($handler, $bundle, $view_mode)) {
314
    $bundle = field_extract_bundle($entity_type, $bundle);
315
    $instances = field_info_instances($entity_type, $bundle);
316

    
317
    // @todo, handle extra fields.
318
    // field_info_extra_fields($entity_type, $bundle, 'display');
319

    
320
    // Look through all the fields and look for fields that aren't hidden.
321
    // As soon as one is found, end this function. Finding any non-hidden
322
    // fields is the point of this function.
323
    foreach ($instances as $instance) {
324
      if (!empty($instance['display'][$view_mode]['type']) && $instance['display'][$view_mode]['type'] !== 'hidden') {
325
        return TRUE;
326
      }
327
    }
328
  }
329

    
330
  return FALSE;
331
}
332

    
333
/**
334
 * Hide all fields on a view mode.
335
 *
336
 * @todo, connect this function to a form and/or ajax callback.
337
 */
338
function panelizer_hide_fields_on_panelized_view_mode($entity_type, $bundle, $view_mode) {
339

    
340
  $bundle = field_extract_bundle($entity_type, $bundle);
341
  $instances = field_info_instances($entity_type, $bundle);
342

    
343
  foreach ($instances as $instance) {
344
    if (!empty($instance['display'][$view_mode]['type'])) {
345
      $instance['display'][$view_mode]['type'] = 'hidden';
346
      field_update_instance($instance);
347
    }
348
  }
349
}
350

    
351
/**
352
 * Implements hook_update_dependencies().
353
 */
354
function panelizer_update_dependencies() {
355
  // Update 7115 requires UUID support in CTools and Panels.
356
  $dependencies['panelizer'][7115] = array(
357
    'panels' => 7302,
358
  );
359

    
360
  // These updates requires Panels storage support, which was added in
361
  // panels_update_7305.
362
  $dependencies['panelizer'][7302] = array(
363
    'panels' => 7305,
364
  );
365
  $dependencies['panelizer'][7303] = array(
366
    'panels' => 7305,
367
  );
368

    
369
  return $dependencies;
370
}
371

    
372
/**
373
 * Update the panelizer variable to be more feature module friendly.
374
 */
375
function panelizer_update_7100() {
376
  $panelizer_defaults = variable_get('panelizer_defaults', array());
377

    
378
  if (!empty($panelizer_defaults)) {
379
    foreach ($panelizer_defaults as $entity => $bundles) {
380
      foreach ($bundles as $bundle => $values) {
381
        variable_set('panelizer_defaults_' . $entity . '_' . $bundle, $values);
382
      }
383
    }
384
  }
385

    
386
  variable_del('panelizer_defaults');
387
  return t('Updated panelizer variables.');
388
}
389

    
390
/**
391
 * Update the panelizer node table to be the panelizer entity table.
392
 */
393
function panelizer_update_7101() {
394
  // Rename the table.
395
  db_rename_table('panelizer_node', 'panelizer_entity');
396

    
397
  // Remove the primary key.
398
  db_drop_primary_key('panelizer_entity');
399

    
400
  // Add the entity type.
401
  $spec = array(
402
    'description' => 'The type of the entity this panel is attached to.',
403
    'type' => 'varchar',
404
    'length' => 255,
405
  );
406
  db_add_field('panelizer_entity', 'entity_type', $spec);
407

    
408
  // Rename nid to entity_id.
409
  $spec = array(
410
    'type' => 'int',
411
    'not null' => TRUE,
412
    'default' => 0,
413
    'description' => 'The entity ID this panel is attached to.',
414
  );
415
  db_change_field('panelizer_entity', 'nid', 'entity_id', $spec);
416

    
417
  // Update the entity_type field to 'node' since all pre-existing
418
  // panelizer objects are nodes.
419
  db_update('panelizer_entity')
420
    ->fields(array('entity_type' => 'node'))
421
    ->execute();
422

    
423
  // Add the new index
424
  db_add_primary_key('panelizer_entity', array('entity_type', 'entity_id'));
425
}
426

    
427
/**
428
 * Add revision support.
429
 */
430
function panelizer_update_7102() {
431
  // Remove the primary key.
432
  db_drop_primary_key('panelizer_entity');
433

    
434
  // Add the revision ID field.
435
  $spec = array(
436
    'description' => 'The revision id of the entity.',
437
    'type' => 'int',
438
    'unsigned' => TRUE,
439
  );
440
  db_add_field('panelizer_entity', 'revision_id', $spec);
441

    
442
  db_query("UPDATE {panelizer_entity} pe LEFT JOIN {node} n ON pe.entity_id = n.nid SET pe.revision_id = n.vid");
443

    
444
  // Add the new index.
445
  db_add_primary_key('panelizer_entity', array('entity_type', 'entity_id', 'revision_id'));
446

    
447
  return t('Added revision support.');
448
}
449

    
450
/**
451
 * Set primary keys to NOT NULL.
452
 */
453
function panelizer_update_7103() {
454
  $spec = array(
455
    'description' => 'The type of the entity this panel is attached to.',
456
    'type' => 'varchar',
457
    'length' => 128,
458
    'not null' => TRUE,
459
  );
460
  db_change_field('panelizer_entity', 'entity_type', 'entity_type', $spec);
461

    
462
  $spec = array(
463
    'description' => 'The revision id of the entity.',
464
    'type' => 'int',
465
    'unsigned' => TRUE,
466
    'not null' => TRUE,
467
  );
468
  db_change_field('panelizer_entity', 'revision_id', 'revision_id', $spec);
469

    
470
  $spec = array(
471
    'type' => 'serial',
472
    'description' => 'The database primary key.',
473
    'no export' => TRUE,
474
    'not null' => TRUE,
475
  );
476
  db_change_field('panelizer_defaults', 'pnid', 'pnid', $spec);
477
}
478

    
479
/**
480
 * Add the {panelizer_defaults}.access field.
481
 */
482
function panelizer_update_7104() {
483
  $spec = array(
484
    'type' => 'text',
485
    'size' => 'big',
486
    'description' => 'Contains the access control for editing this default.',
487
    'serialize' => TRUE,
488
    'object default' => array(),
489
  );
490
  db_add_field('panelizer_defaults', 'access', $spec);
491
}
492

    
493
/**
494
 * Add the view mode field.
495
 */
496
function panelizer_update_7105() {
497
  $spec = array(
498
    'type' => 'varchar',
499
    'length' => '128',
500
    'description' => 'Contains the view mode this panelizer is for.',
501
    'not null' => TRUE,
502
  );
503
  db_add_field('panelizer_defaults', 'view_mode', $spec);
504
  db_add_field('panelizer_entity', 'view_mode', $spec);
505

    
506
  db_update('panelizer_defaults')
507
    ->fields(array(
508
      'view_mode' => 'page_manager',
509
    ))
510
    ->execute();
511

    
512
  db_update('panelizer_entity')
513
    ->fields(array(
514
      'view_mode' => 'page_manager',
515
    ))
516
    ->execute();
517
}
518

    
519
/**
520
 * Add the view_mode to the primary key for the {panelizer_entity} table.
521
 */
522
function panelizer_update_7106() {
523
  db_drop_primary_key('panelizer_entity');
524

    
525
  if (db_index_exists('panelizer_entity', 'PRIMARY')) {
526
    throw new DrupalUpdateException(t('Could not drop the panelizer_entity primary key in order to recreate it.'));
527
  }
528

    
529
  // Add the new index.
530
  db_add_primary_key('panelizer_entity', array(
531
    'entity_type',
532
    'entity_id',
533
    'revision_id',
534
    'view_mode',
535
  ));
536

    
537
  if (db_index_exists('panelizer_entity', 'PRIMARY')) {
538
    return t('The Panelizer database scheme has been updated.');
539
  }
540
  else {
541
    throw new DrupalUpdateException(t('There was a problem recreating the panelizer_entity primary key.'));
542
  }
543
}
544

    
545
/**
546
 * Add the css class and element title fields.
547
 */
548
function panelizer_update_7107() {
549
  $spec = array(
550
    'type' => 'varchar',
551
    'length' => '255',
552
    'description' => 'The CSS class this panel should use.',
553
    'default' => '',
554
  );
555
  db_add_field('panelizer_defaults', 'css_class', $spec);
556
  db_add_field('panelizer_entity', 'css_class', $spec);
557

    
558
  $spec = array(
559
    'type' => 'varchar',
560
    'length' => '255',
561
    'description' => 'The HTML element the title should use.',
562
    'default' => 'H2',
563
  );
564
  db_add_field('panelizer_defaults', 'title_element', $spec);
565
  db_add_field('panelizer_entity', 'title_element', $spec);
566
}
567

    
568
/**
569
 * Add the link_to_entity field
570
 */
571
function panelizer_update_7108() {
572
  $spec = array(
573
    'type' => 'int',
574
    'size' => 'tiny',
575
    'description' => 'Whether or not the title should link to the entity.',
576
    'default' => 1,
577
  );
578
  db_add_field('panelizer_defaults', 'link_to_entity', $spec);
579
  db_add_field('panelizer_entity', 'link_to_entity', $spec);
580
}
581

    
582
/**
583
 * Add the extra field so that modules can extend panelizer more easily.
584
 */
585
function panelizer_update_7109() {
586
  $spec = array(
587
    'type' => 'text',
588
    'size' => 'big',
589
    'description' => 'Contains extra data that can be added by modules.',
590
    'serialize' => TRUE,
591
    'object default' => array(),
592
  );
593
  db_add_field('panelizer_defaults', 'extra', $spec);
594
  db_add_field('panelizer_entity', 'extra', $spec);
595
}
596

    
597
/**
598
 * Changing the size of the entity_type field of panelizer_entity.
599
 */
600
function panelizer_update_7110() {
601
  db_drop_primary_key('panelizer_entity');
602
  $spec = array(
603
    'description' => 'The type of the entity this panel is attached to.',
604
    'type' => 'varchar',
605
    'length' => 128,
606
    'not null' => TRUE,
607
  );
608
  $keys = array(
609
    'primary key' => array('entity_type', 'entity_id', 'revision_id', 'view_mode'),
610
  );
611
  db_change_field('panelizer_entity', 'entity_type', 'entity_type', $spec, $keys);
612
}
613

    
614
/**
615
 * This update script was removed, nothing to see here.
616
 */
617
function panelizer_update_7111() {
618
  // Nothing.
619
}
620

    
621
/**
622
 * Fix Panelizer settings.
623
 */
624
function panelizer_update_7112() {
625
  foreach (entity_get_info() as $entity_type => $entity_info) {
626
    if (!empty($entity_info) && !empty($entity_info['bundles'])) {
627
      foreach ($entity_info['bundles'] as $bundle => &$bundle_info) {
628
        $var_name = 'panelizer_defaults_' . $entity_type . '_' . $bundle;
629
        $settings = variable_get($var_name);
630
        if (!empty($settings) && !empty($settings['view modes'])) {
631
          foreach ($settings['view modes'] as $view_mode => &$config) {
632
            // If the bundle itself or this view mode are disabled, make sure
633
            // all settings are disabled.
634
            if (empty($settings['status']) || empty($config['status'])) {
635
              foreach ($config as $key => $val) {
636
                $config[$key] = 0;
637
              }
638
            }
639
          }
640
          // Update the settings.
641
          variable_set($var_name, $settings);
642
        }
643
      }
644
    }
645
  }
646
}
647

    
648
/**
649
 * Make {panelizer_entity}.view_mode NOT NULL.
650
 */
651
function panelizer_update_7113() {
652
  db_drop_primary_key('panelizer_entity');
653

    
654
  $spec = array(
655
    'description' => 'Contains the view mode this panelizer is for.',
656
    'type' => 'varchar',
657
    'length' => '128',
658
    'not null' => TRUE,
659
  );
660
  $keys = array('primary key' =>
661
    array('entity_type', 'entity_id', 'revision_id', 'view_mode'),
662
  );
663
  db_change_field('panelizer_entity', 'view_mode', 'view_mode', $spec, $keys);
664
}
665

    
666
/**
667
 * Clear the menu cache to fix the display actions page arguments.
668
 */
669
function panelizer_update_7114() {
670
  variable_set('menu_rebuild_needed', TRUE);
671
}
672

    
673
/**
674
 * Ensure that each Panelizer display is only used once, so that each
675
 * revision has a separate record.
676
 */
677
function panelizer_update_7115(&$sandbox) {
678
  // This loops through all of the records in {panelizer_entity}, looks for any
679
  // displays that are used more than once and clones any additional copies
680
  // that are needed. The goal is to have each display only used once.
681

    
682
  // Process records by groups of 1 (arbitrary value). Doing this one at a time
683
  // because some sites can have a LOT of revisions.
684
  $limit = 1;
685

    
686
  // When ran through Drush it's Ok to process a larger number of objects at a
687
  // time.
688
  if (drupal_is_cli()) {
689
    $limit = 10;
690
  }
691

    
692
  // The update hasn't been ran before.
693
  if (!isset($sandbox['progress'])) {
694
    // The count of records visited so far.
695
    $sandbox['progress'] = 0;
696

    
697
    // Load any 'did' values that are used in more than one revision.
698
    $records = db_query("SELECT did
699
      FROM {panelizer_entity}
700
      WHERE revision_id > 0 AND did <> 0 AND did IS NOT NULL
701
      GROUP BY did
702
      HAVING count(revision_id) > 1");
703

    
704
    // If there are no records, there's nothing to do.
705
    if ($records->rowCount() == 0) {
706
      return t('No Panelizer display records need fixing.');
707
    }
708

    
709
    // Total records that must be processed.
710
    $sandbox['max'] = $records->rowCount();
711

    
712
    watchdog('panelizer', 'Need to fix @count duplicate displays.', array('@count' => $records->rowCount()));
713
  }
714

    
715
  // Loop through the records in smaller chunks.
716
  $dids = db_query_range("SELECT did
717
    FROM {panelizer_entity}
718
    WHERE revision_id > 0 AND did <> 0 AND did IS NOT NULL
719
    GROUP BY did
720
    HAVING count(revision_id) > 1", 0, $limit)->fetchCol();
721

    
722
  // Track the entities that need to be reset.
723
  $cache_clear = array();
724

    
725
  ctools_include('plugins', 'panels');
726
  ctools_include('content');
727

    
728
  // Load all of the requested displays.
729
  foreach (panels_load_displays($dids) as $original_did => $display) {
730
    // Load each panelizer_entity record for this display.
731
    $panelizers = db_query("SELECT * FROM {panelizer_entity} WHERE did=:did", array(':did' => $display->did));
732
    $ctr = 0;
733
    foreach ($panelizers as $panelizer) {
734
      $ctr++;
735
      // Skip the first record.
736
      if ($ctr === 1) {
737
        $ctr++;
738
        continue;
739
      }
740

    
741
      // Reset the 'did' value so that a new record can be created.
742
      unset($display->did);
743

    
744
      // Regenerate the UUID.
745
      $display->uuid = ctools_uuid_generate();
746

    
747
      // Save the display, thus creating a new 'did' value. Also, using
748
      // db_insert() as it's safer than using drupal_write_record() directly
749
      // during an update script. Also, doing each field individually to avoid
750
      // corrupting data during hook_panels_display_save.
751
      $display->did = db_insert('panels_display')
752
        ->fields(array(
753
          'did' => NULL,
754
          'layout' => $display->layout,
755
          'layout_settings' => serialize($display->layout_settings),
756
          'panel_settings' => serialize($display->panel_settings),
757
          'cache' => serialize($display->cache),
758
          'title' => $display->title,
759
          'hide_title' => $display->hide_title,
760
          'title_pane' => $display->title_pane,
761
          'uuid' => $display->uuid,
762
        ))
763
        ->execute();
764
      $message = 'Panelizer update 7115: created display %did for %entity_type %entity_id';
765
      $message_args = array(
766
        '%did' => $display->did,
767
        '%entity_type' => $panelizer->entity_type,
768
        '%entity_id' => $panelizer->entity_id,
769
      );
770
      watchdog('panelizer', $message, $message_args, WATCHDOG_NOTICE);
771

    
772
      // Reset the 'pid' values of each pane, using a new UUID. Because its
773
      // non-numeric, when the display is saved it'll create a new record for
774
      // each pane, but still keep all of the internal pointers accurate.
775
      foreach ($display->panels as $region => $panes) {
776
        foreach ((array) $panes as $position => $pid) {
777
          // Pane not found. Shouldn't happen, but you never know.
778
          if (!isset($display->content[$pid])) {
779
            watchdog('panelizer', 'Panelizer update 7115: couldn\'t load pane %pid for display %did', array('%pid' => $pid, '%did' => $display->did), WATCHDOG_WARNING);
780
            continue;
781
          }
782

    
783
          // Load the pane.
784
          $new_pane = clone $display->content[$pid];
785

    
786
          // This appears to only serve the purpose of ensuring necessary APIs
787
          // and include files are loaded.
788
          ctools_get_content_type($new_pane->type);
789

    
790
          // Remove the pid, it'll be created during the database insertion.
791
          unset($new_pane->pid);
792

    
793
          // Generate a new UUID.
794
          $new_pane->uuid = ctools_uuid_generate();
795

    
796
          // Update the pane's did.
797
          $new_pane->did = $display->did;
798

    
799
          // Create the pane record, save the return ID as the pane pid. Again,
800
          // using db_insert directly because it's safer than
801
          // drupal_write_record during an update script. Also, doing the fields
802
          // individually so that the data isn't corrupted during
803
          // hook_panels_pane_insert.
804
          $new_pane->pid = db_insert('panels_pane')
805
            ->fields(array(
806
              'pid' => NULL,
807
              'did' => $new_pane->did,
808
              'panel' => $new_pane->panel,
809
              'type' => $new_pane->type,
810
              'subtype' => $new_pane->subtype,
811
              'shown' => $new_pane->shown,
812
              'access' => serialize($new_pane->access),
813
              'configuration' => serialize($new_pane->configuration),
814
              'cache' => serialize($new_pane->cache),
815
              'style' => serialize($new_pane->style),
816
              'css' => serialize($new_pane->css),
817
              'extras' => serialize($new_pane->extras),
818
              'locks' => serialize($new_pane->locks),
819
              'uuid' => $new_pane->uuid,
820
            ))
821
            ->execute();
822

    
823
          // Tell the world.
824
          module_invoke_all('panels_pane_insert', $new_pane);
825
          watchdog('panelizer', 'Panelizer update 7115: created pane %pid for display %did', array('%pid' => $new_pane->pid, '%did' => $new_pane->did), WATCHDOG_NOTICE);
826

    
827
          // Update the optional title pane.
828
          if (isset($display->title_pane) && $display->title_pane == $pid) {
829
            $display->title_pane = $new_pane->pid;
830
            // Do a simple update query to write it so we don't have to rewrite
831
            // the whole record. We can't just save writing the whole record
832
            // here because it was needed to get the did. Chicken, egg, more
833
            // chicken.
834
            db_update('panels_display')
835
              ->fields(array(
836
                'title_pane' => $new_pane->pid
837
              ))
838
              ->condition('did', $display->did)
839
              ->execute();
840
          }
841
        }
842
      }
843

    
844
      // Allow other modules to take action when a display is saved.
845
      module_invoke_all('panels_display_save', $display);
846

    
847
      // Assign the new display to this Panelizer record.
848
      $panelizer->did = $display->did;
849

    
850
      // Update the {panelizer} record.
851
      db_update('panelizer_entity')
852
        ->fields((array) $panelizer)
853
        ->condition('entity_type', $panelizer->entity_type)
854
        ->condition('entity_id', $panelizer->entity_id)
855
        ->condition('revision_id', $panelizer->revision_id)
856
        ->condition('view_mode', $panelizer->view_mode)
857
        ->execute();
858

    
859
      // Clear this entity's cache.
860
      $cache_clear[$panelizer->entity_type][] = $panelizer->entity_id;
861

    
862
    }
863
    watchdog('panelizer', 'Panelizer update 7115: finished fixing %did', array('%did' => $original_did), WATCHDOG_NOTICE);
864

    
865
    $sandbox['progress']++;
866
  }
867

    
868
  // Clear the caches for any entities that are updated.
869
  foreach ($cache_clear as $entity_type => $entity_ids) {
870
    entity_get_controller($entity_type)->resetCache($entity_ids);
871
  }
872

    
873
  $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
874

    
875
  return t('Fixed @count Panelizer record(s) (out of @total) that were using the same display.', array('@count' => $sandbox['progress'], '@total' => $sandbox['max']));
876
}
877

    
878
/**
879
 * Notify the site builder that the "Panelizer" tabs have been renamed.
880
 */
881
function panelizer_update_7116() {
882
  drupal_set_message(t('Note: the "Panelizer" tabs on content, user, term pages, etc have been renamed to "Customize display".'));
883
}
884

    
885
/**
886
 * Clear the menu cache to pull in the new menu paths.
887
 */
888
function panelizer_update_7117() {
889
  variable_set('menu_rebuild_needed', TRUE);
890
  drupal_set_message(t('Note: the main Panelizer configuration page has moved to the "Structure" menu instead of the "Config" menu.'));
891
}
892

    
893
/**
894
 * Reload the menus to fix the 'content' default local tasks.
895
 */
896
function panelizer_update_7118() {
897
  variable_set('menu_rebuild_needed', TRUE);
898
}
899

    
900
/**
901
 * Fix any {panelizer_entity} records that were broken by update 7115. This may
902
 * take some time.
903
 */
904
function panelizer_update_7119(&$sandbox) {
905
  // Process records by groups of 10 (arbitrary value).
906
  $limit = 10;
907
  // When ran through Drush it's Ok to process a larger number of objects at a
908
  // time.
909
  if (drupal_is_cli()) {
910
    $limit = 100;
911
  }
912

    
913
  // The update hasn't been ran before.
914
  if (!isset($sandbox['progress'])) {
915
    // The count of records visited so far.
916
    $sandbox['progress'] = 0;
917

    
918
    // Load any 'panelizer_entity' values that are corrupt.
919
    $records = db_query("SELECT DISTINCT entity_type, entity_id
920
      FROM {panelizer_entity}
921
      WHERE contexts LIKE 's:%'
922
        OR relationships LIKE 's:%'
923
        OR extra LIKE 's:%'");
924

    
925
    // If there are no records, there's nothing to do.
926
    if ($records->rowCount() == 0) {
927
      return t('No {panelizer_entity} records were corrupted by update 7115.');
928
    }
929

    
930
    // Total records that must be processed.
931
    $sandbox['max'] = $records->rowCount();
932

    
933
    watchdog('panelizer', 'Need to fix {panelizer_entity} records for @count entities that were damaged by update 7115.', array('@count' => $records->rowCount()));
934
  }
935

    
936
  // Get a small batch of records.
937
  $records = db_query_range("SELECT DISTINCT entity_type, entity_id
938
    FROM {panelizer_entity}
939
    WHERE contexts LIKE 's:%'
940
      OR relationships LIKE 's:%'
941
      OR extra LIKE 's:%'", 0, $limit);
942

    
943
  // Loop over each record.
944
  foreach ($records as $record) {
945
    // Track whether this entity needs to be fixed.
946
    $entity_updated = FALSE;
947

    
948
    // Get each {panelizer_record} for this entity.
949
    $entity_records = db_query("SELECT *
950
      FROM {panelizer_entity}
951
      WHERE entity_type = :entity_type
952
        AND entity_id = :entity_id
953
      ORDER BY revision_id", (array)$record);
954
    foreach ($entity_records as $panelizer) {
955
      $last_display = NULL;
956

    
957
      // Unserialize each of the serialized values.
958
      foreach (array('contexts', 'relationships', 'extra') as $val) {
959
        $panelizer->$val = unserialize($panelizer->$val);
960
      }
961

    
962
      // Keep track of whether the record needs to be saved.
963
      $panelizer_updated = FALSE;
964

    
965
      // Verify each of the items is an array or if it was double-serialized.
966
      foreach (array('contexts', 'relationships', 'extra') as $val) {
967
        if (is_string($panelizer->$val)) {
968
          $panelizer->$val = unserialize($panelizer->$val);
969
          $panelizer_updated = TRUE;
970
          $entity_updated = TRUE;
971
        }
972
      }
973

    
974
      // Update the {panelizer_entity} record.
975
      if ($panelizer_updated) {
976
        db_update('panelizer_entity')
977
          ->fields(array(
978
            'contexts' => serialize($panelizer->contexts),
979
            'relationships' => serialize($panelizer->relationships),
980
            'extra' => serialize($panelizer->extra),
981
          ))
982
          ->condition('entity_type', $panelizer->entity_type)
983
          ->condition('entity_id', $panelizer->entity_id)
984
          ->condition('revision_id', $panelizer->revision_id)
985
          ->condition('view_mode', $panelizer->view_mode)
986
          ->execute();
987
      }
988
    }
989

    
990
    // The entity was updated so clear its cache.
991
    if ($entity_updated) {
992
      entity_get_controller($panelizer->entity_type)->resetCache(array($panelizer->entity_id));
993
    }
994

    
995
    $sandbox['progress']++;
996
  }
997

    
998
  $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
999

    
1000
  return t('Fixed {panelizer_entity} records for @count entities out of @total total.', array('@count' => $sandbox['progress'], '@total' => $sandbox['max']));
1001
}
1002

    
1003
/**
1004
 * Update 7115 may have resulted in {panels_pane} records not being created
1005
 * properly. This will create them, and may take some time.
1006
 */
1007
function panelizer_update_7120(&$sandbox) {
1008
  // Process records by groups of 1 (arbitrary value). Doing this one at a time
1009
  // because some sites can have a LOT of revisions.
1010
  $limit = 1;
1011

    
1012
  // When ran through Drush it's Ok to process a larger number of objects at a
1013
  // time.
1014
  if (drupal_is_cli()) {
1015
    $limit = 10;
1016
  }
1017

    
1018
  // The update hasn't been ran before.
1019
  if (!isset($sandbox['progress'])) {
1020
    // The count of records visited so far.
1021
    $sandbox['progress'] = 0;
1022

    
1023
    // Load any 'panelizer_entity' values that are corrupt.
1024
    $records = db_query("SELECT DISTINCT entity_type, entity_id
1025
      FROM {panelizer_entity} pe
1026
        LEFT OUTER JOIN {panels_pane} pp
1027
          ON pe.did = pp.did
1028
      WHERE pe.did > 0
1029
        AND pp.did IS NULL");
1030

    
1031
    // If there are no records, there's nothing to do.
1032
    if ($records->rowCount() == 0) {
1033
      return t('No panes were lost by update 7115.');
1034
    }
1035

    
1036
    // Total records that must be processed.
1037
    $sandbox['max'] = $records->rowCount();
1038

    
1039
    watchdog('panelizer', 'Need to fix panes for @count entities that were lost by update 7115.', array('@count' => $records->rowCount()));
1040
  }
1041

    
1042
  // Get a small batch of records.
1043
  $records = db_query_range("SELECT DISTINCT entity_type, entity_id
1044
    FROM {panelizer_entity} pe
1045
      LEFT OUTER JOIN {panels_pane} pp
1046
        ON pe.did = pp.did
1047
    WHERE pe.did > 0
1048
      AND pp.did IS NULL", 0, $limit);
1049

    
1050
  watchdog('panelizer', 'Fixing panes for @count entities that were lost by update 7115.', array('@count' => $records->rowCount()));
1051

    
1052
  // Loop over each record.
1053
  foreach ($records as $record) {
1054
    // Track whether this entity was changed.
1055
    $entity_updated = FALSE;
1056

    
1057
    // Keep track of the last display for each entity.
1058
    $last_display = NULL;
1059

    
1060
    // Get each {panelizer_record} for this entity. Load these in REVERSE order
1061
    // because the panes were moved to the last revision.
1062
    $entity_records = db_query("SELECT *
1063
      FROM {panelizer_entity}
1064
      WHERE entity_type = :entity_type
1065
        AND entity_id = :entity_id
1066
      ORDER BY revision_id DESC", (array)$record);
1067
    foreach ($entity_records as $panelizer) {
1068
      // If this is a custom display, load it.
1069
      if (!empty($panelizer->did)) {
1070
        $display = panels_load_display($panelizer->did);
1071

    
1072
        // Check if the display is bereft of panes.
1073
        if (empty($display->content)) {
1074
          // Hopefully the "last display" won't be blank.
1075
          if (empty($last_display)) {
1076
            watchdog('panelizer', "Unable to load records for display did for entity_type entity_id. Sorry.", (array)$panelizer);
1077
          }
1078
          else {
1079
            foreach ($last_display->content as $pane) {
1080
              // Clone the pane to avoid accidental damage.
1081
              $new_pane = clone $pane;
1082

    
1083
              // Erase the pid so a new record can be saved.
1084
              $new_pane->pid = NULL;
1085

    
1086
              // Tie the pane to this display.
1087
              $new_pane->did = $display->did;
1088

    
1089
              // Update the pane's UUID.
1090
              $new_pane->uuid = ctools_uuid_generate();
1091

    
1092
              // Serialize some of the fields prior to saving.
1093
              foreach (array('access', 'configuration', 'cache', 'style', 'css', 'extras', 'locks') as $val) {
1094
                $new_pane->$val = serialize($new_pane->$val);
1095
              }
1096

    
1097
              // Create the pane record.
1098
              db_insert('panels_pane')
1099
                ->fields((array)$new_pane)
1100
                ->execute();
1101

    
1102
              // Tell the world.
1103
              module_invoke_all('panels_pane_insert', $new_pane);
1104
            }
1105
          }
1106
        }
1107

    
1108
        // This display included all of its panes, so skip the fixing and just
1109
        // use it as the 'last' display for the next loop.
1110
        else {
1111
          $last_display = $display;
1112
        }
1113
      }
1114
    }
1115

    
1116
    // The entity was updated, to clear its cache.
1117
    if ($entity_updated) {
1118
      entity_get_controller($panelizer->entity_type)->resetCache(array($panelizer->entity_id));
1119
    }
1120

    
1121
    $sandbox['progress']++;
1122
  }
1123

    
1124
  $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
1125

    
1126
  return t('Recovered panes for @count entities out of @total total.', array('@count' => $sandbox['progress'], '@total' => $sandbox['max']));
1127
}
1128

    
1129
/**
1130
 * Fix the 'selection' variables.
1131
 */
1132
function panelizer_update_7121() {
1133
  foreach (entity_get_info() as $entity_type => $entity_info) {
1134
    if (isset($entity_info['bundles'])) {
1135
      foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
1136
        // View mode variables.
1137
        if (!empty($entity_info['view modes'])) {
1138
          foreach ($entity_info['view modes'] as $view_mode => $view_info) {
1139
            $var_name = 'panelizer_' . $entity_type . ':' . $bundle_name . ':' . $view_mode . '_selection';
1140
            $var = variable_get($var_name);
1141
            if (empty($var)) {
1142
              variable_del($var_name);
1143
            }
1144
          }
1145
        }
1146
      }
1147
    }
1148
  }
1149
}
1150

    
1151
/**
1152
 * Remove {panelizer_entity} records with only default values.
1153
 */
1154
function panelizer_update_7300(&$sandbox) {
1155
  // Initialize sandbox.
1156
  if (empty($sandbox)) {
1157
    $sandbox['progress'] = 0;
1158
    $sandbox['processed_entity_types'] = array();
1159

    
1160
    // Get a list of all records using a default display.
1161
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT entity_type) FROM {panelizer_entity} WHERE did = 0')->fetchField();
1162

    
1163
    // Bail if no records found.
1164
    if (empty($sandbox['max'])) {
1165
      return t('No records need to be fixed.');
1166
    }
1167

    
1168
    watchdog('panelizer', 'Default panelizer records will be removed for @count entity types.', array('@count' => $sandbox['max']));
1169
  }
1170

    
1171
  // Main sandbox query and loop.  Processes one entity type at a time.
1172
  $query = db_select('panelizer_entity', 'pe')
1173
    ->distinct()
1174
    ->fields('pe', array('entity_type'));
1175
  if (!empty($sandbox['processed_entity_types'])) {
1176
    $query->condition('entity_type', $sandbox['processed_entity_types'], 'NOT IN');
1177
  }
1178
  $entity_types = $query->execute()->fetchCol();
1179
  foreach ($entity_types as $entity_type) {
1180
    // Get the default panelizer names for the entity type.
1181
    $default_names = array();
1182
    $entity_info = entity_get_info($entity_type);
1183
    if (!empty($entity_info) && !empty($entity_info['bundles'])) {
1184
      foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
1185
        $var_name = 'panelizer_defaults_' . $entity_type . '_' . $bundle;
1186
        $settings = variable_get($var_name);
1187
        if (!empty($settings) && !empty($settings['view modes'])) {
1188
          foreach ($settings['view modes'] as $view_mode => $config) {
1189
            $default_name = implode(':', array($entity_type, $bundle, 'default'));
1190
            if ($view_mode != 'page_manager') {
1191
              $default_name .= ':' . $view_mode;
1192
            }
1193
            $default_names[] = $default_name;
1194
          }
1195
        }
1196
      }
1197
    }
1198

    
1199
    // Delete panelizer records that have one of the default names.
1200
    $deleted = db_delete('panelizer_entity')
1201
      ->condition('name', $default_names, 'IN')
1202
      ->condition('entity_type', $entity_type)
1203
      ->execute();
1204
    if ($deleted > 0) {
1205
      watchdog('panelizer', '@count default panelizer records were removed for entity type: @entity_type.', array('@count' => $deleted, '@entity_type' => $entity_type));
1206
    }
1207
    else {
1208
      watchdog('panelizer', 'No default panelizer records were found for entity type: @entity_type.', array('@entity_type' => $entity_type));
1209
    }
1210

    
1211
    // Update sandbox progress.
1212
    $sandbox['progress']++;
1213
    $sandbox['processed_entity_types'][] = $entity_type;
1214
  }
1215

    
1216
  if ($sandbox['progress'] != $sandbox['max']) {
1217
    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
1218
  }
1219
}
1220

    
1221
/**
1222
 * Add an index to {panelizer_entity} revision IDs.
1223
 */
1224
function panelizer_update_7301() {
1225
  if (!db_index_exists('panelizer_entity', 'revision_id')) {
1226
    db_add_index('panelizer_entity', 'revision_id', array('revision_id'));
1227
    return t('Added an extra index on the {panelizer_entity} table for the revision_id column.');
1228
  }
1229
  else {
1230
    return t('There was already an index on the {panelizer_entity} table for the revision_id column, so nothing was done.');
1231
  }
1232
}
1233

    
1234
/**
1235
 * Set the storage type and ID on existing {panelizer_default} records.
1236
 */
1237
function panelizer_update_7302(&$sandbox) {
1238
  if (!isset($sandbox['progress'])) {
1239
    // Initialize batch update information.
1240
    $sandbox['progress'] = (float)0;
1241
    $sandbox['current_did'] = -1;
1242
    $sandbox['max'] = db_query("SELECT COUNT(pd.did)
1243
      FROM {panels_display} pd
1244
        JOIN {panelizer_defaults} p ON p.did = pd.did
1245
      WHERE pd.storage_type = ''")->fetchField();
1246
    if (empty($sandbox['max'])) {
1247
      return t('Nothing to be fixed in Panelizer update 7302.');
1248
    }
1249
  }
1250

    
1251
  // Set a limit of how many rows to process per batch. 1000 rows is probably
1252
  // good.
1253
  $limit = 1000;
1254

    
1255
  // Run the query
1256
  $result = db_query_range("SELECT pd.did, p.name
1257
    FROM {panels_display} pd
1258
      JOIN {panelizer_defaults} p ON p.did = pd.did
1259
    WHERE pd.storage_type = '' AND pd.did > :current_did", 0, $limit, array(':current_did' => $sandbox['current_did']));
1260

    
1261
  foreach ($result as $row) {
1262
    db_update('panels_display')
1263
      ->fields(array(
1264
        'storage_type' => 'panelizer_default',
1265
        'storage_id' => $row->name,
1266
      ))
1267
      ->condition('did', $row->did)
1268
      ->execute();
1269

    
1270
    // Update our progress information.
1271
    $sandbox['progress']++;
1272
    $sandbox['current_did'] = $row->did;
1273
  }
1274

    
1275
  // Set the "finished" status, to tell batch engine whether this function
1276
  // needs to run again.
1277
  $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
1278

    
1279
  if ($sandbox['#finished']) {
1280
    return t('Added the storage type for panelizer_defaults to relevant panels displays');
1281
  }
1282
}
1283

    
1284
/**
1285
 * Set the storage type and ID on existing {panelizer_entity} records.
1286
 */
1287
function panelizer_update_7303(&$sandbox) {
1288
  if (!isset($sandbox['progress'])) {
1289
    // Initialize batch update information.
1290
    $sandbox['progress'] = (float)0;
1291
    $sandbox['current_did'] = -1;
1292
    $sandbox['max'] = db_query("SELECT COUNT(pd.did)
1293
      FROM {panels_display} pd
1294
        JOIN {panelizer_entity} p ON p.did = pd.did
1295
      WHERE pd.storage_type = ''")->fetchField();
1296
    if (empty($sandbox['max'])) {
1297
      return t('Nothing to be fixed in Panelizer update 7303.');
1298
    }
1299
  }
1300

    
1301
  // Set a limit of how many rows to process per batch.
1302
  $limit = 1000;
1303

    
1304
  // Look for records to be updated.
1305
  $result = db_query_range("SELECT pd.did, p.entity_type, p.entity_id, p.view_mode
1306
    FROM {panels_display} pd
1307
      JOIN {panelizer_entity} p ON p.did = pd.did
1308
    WHERE pd.storage_type = '' AND pd.did > :current_did", 0, $limit, array(':current_did' => $sandbox['current_did']));
1309

    
1310
  foreach ($result as $row) {
1311
    db_update('panels_display')
1312
      ->fields(array(
1313
        'storage_type' => 'panelizer_entity',
1314
        'storage_id' => implode(':', array($row->entity_type, $row->entity_id, $row->view_mode)),
1315
      ))
1316
      ->condition('did', $row->did)
1317
      ->execute();
1318

    
1319
    // Update our progress information.
1320
    $sandbox['progress']++;
1321
    $sandbox['current_did'] = $row->did;
1322
  }
1323

    
1324
  // Set the "finished" status, to tell batch engine whether this function
1325
  // needs to run again.
1326
  $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
1327

    
1328
  if ($sandbox['#finished']) {
1329
    return t('Added the storage type for panelizer_entities to relevant panels displays');
1330
  }
1331
}