block.module 24 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?php
Dries's avatar
 
Dries committed
2

Dries's avatar
   
Dries committed
3
4
/**
 * @file
5
 * Controls the visual building blocks a page is constructed with.
Dries's avatar
   
Dries committed
6
7
 */

8
use Drupal\Component\Plugin\Exception\PluginException;
9
use Drupal\Component\Utility\NestedArray;
10

11
12
13
/**
 * Denotes that a block is not enabled in any region and should not be shown.
 */
14
const BLOCK_REGION_NONE = -1;
15

16
17
18
/**
 * Users cannot control whether or not they see this block.
 */
19
const BLOCK_CUSTOM_FIXED = 0;
20
21

/**
22
 *  Shows this block by default, but lets individual users hide it.
23
 */
24
const BLOCK_CUSTOM_ENABLED = 1;
25
26

/**
27
 * Hides this block by default but lets individual users show it.
28
 */
29
const BLOCK_CUSTOM_DISABLED = 2;
30
31

/**
32
 * Shows this block on every page except the listed pages.
33
 */
34
const BLOCK_VISIBILITY_NOTLISTED = 0;
35
36

/**
37
 * Shows this block on only the listed pages.
38
 */
39
const BLOCK_VISIBILITY_LISTED = 1;
40
41

/**
42
 * Shows this block if the associated PHP code returns TRUE.
43
 */
44
const BLOCK_VISIBILITY_PHP = 2;
45

46
47
48
49
50
/**
 * Indicates the block label (title) should be displayed to end users.
 */
const BLOCK_LABEL_VISIBLE = 'visible';

Dries's avatar
   
Dries committed
51
/**
52
 * Implements hook_help().
Dries's avatar
   
Dries committed
53
 */
54
55
function block_help($path, $arg) {
  switch ($path) {
Dries's avatar
   
Dries committed
56
    case 'admin/help#block':
57
      $output = '';
58
      $output .= '<h3>' . t('About') . '</h3>';
59
      $output .= '<p>' . t('The Block module allows you to create boxes of content, which are rendered into an area, or region, of one or more pages of a website. The core Seven administration theme, for example, implements the regions "Content" and "Help", and a block may appear in either of these regions. The <a href="@blocks">Blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. For more information, see the online handbook entry for <a href="@block">Block module</a>.', array('@block' => 'http://drupal.org/documentation/modules/block', '@blocks' => url('admin/structure/block'))) . '</p>';
60
61
62
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Positioning content') . '</dt>';
63
      $output .= '<dd>' . t('When working with blocks, remember that all themes do <em>not</em> implement the same regions, or display regions in the same way. Blocks are positioned on a per-theme basis. Users with the <em>Administer blocks</em> permission can disable blocks. Disabled blocks are listed on the <a href="@blocks">Blocks administration page</a>, but are not displayed in any region.', array('@block' => 'http://drupal.org/documentation/modules/block', '@blocks' => url('admin/structure/block'))) . '</dd>';
64
      $output .= '<dt>' . t('Controlling visibility') . '</dt>';
65
66
67
      $output .= '<dd>' . t('Blocks can be configured to be visible only on certain pages, only to users of certain roles, or only on pages displaying certain <a href="@content-type">content types</a>. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.', array('@content-type' => url('admin/structure/types'), '@user' => url('user'))) . '</dd>';
      if (module_exists('custom_block')) {
        $output .= '<dt>' . t('Creating custom blocks') . '</dt>';
68
        $output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can <a href="@block-add">add custom blocks</a>, which are then listed on the <a href="@blocks">Blocks administration page</a>. Once created, custom blocks behave just like default and module-generated blocks.', array('@blocks' => url('admin/structure/block'), '@block-add' => url('admin/structure/block/list/block_plugin_ui:' . config('system.theme')->get('default') . '/add/custom_blocks'))) . '</dd>';
69
      }
70
      $output .= '</dl>';
71
      return $output;
Dries's avatar
   
Dries committed
72
  }
73
74
75
76
77
  if ($arg[0] == 'admin' && $arg[1] == 'structure' && $arg['2'] == 'block' && (empty($arg[3]) || $arg[3] == 'list') && empty($arg[5])) {
    if (!empty($arg[4])) {
      list(, $demo_theme) = explode(':', $arg[4]);
    }
    else {
78
      $demo_theme = config('system.theme')->get('default');
79
    }
80
81
82
83
84
    $themes = list_themes();
    $output = '<p>' . t('This page provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. Since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis. Remember that your changes will not be saved until you click the <em>Save blocks</em> button at the bottom of the page. Click the <em>configure</em> link next to each block to configure its specific title and visibility settings.') . '</p>';
    $output .= '<p>' . l(t('Demonstrate block regions (@theme)', array('@theme' => $themes[$demo_theme]->info['name'])), 'admin/structure/block/demo/' . $demo_theme) . '</p>';
    return $output;
  }
Dries's avatar
   
Dries committed
85
86
}

87
/**
88
 * Implements hook_theme().
89
90
91
 */
function block_theme() {
  return array(
92
    'block' => array(
93
      'render element' => 'elements',
94
      'template' => 'block',
95
    ),
96
97
98
  );
}

Dries's avatar
   
Dries committed
99
/**
100
 * Implements hook_permission().
Dries's avatar
   
Dries committed
101
 */
102
function block_permission() {
103
  return array(
104
105
106
    'administer blocks' => array(
      'title' => t('Administer blocks'),
    ),
107
  );
Dries's avatar
   
Dries committed
108
109
}

Dries's avatar
   
Dries committed
110
/**
111
 * Implements hook_menu().
112
113
 *
 * @todo Clarify the documentation for the per-plugin block admin links.
Dries's avatar
   
Dries committed
114
 */
115
function block_menu() {
116
  $default_theme = config('system.theme')->get('default');
117
  $items['admin/structure/block'] = array(
118
119
    'title' => 'Blocks',
    'description' => 'Configure what block content appears in your site\'s sidebars and other regions.',
120
    'page callback' => 'block_admin_display',
121
    'page arguments' => array($default_theme),
122
    'access arguments' => array('administer blocks'),
123
    'file' => 'block.admin.inc',
124
  );
125
  $items['admin/structure/block/add/%/%'] = array(
126
    'title' => 'Configure block',
127
128
129
130
131
132
133
134
135
    'page callback' => 'block_admin_add',
    'page arguments' => array(4, 5),
    'access arguments' => array('administer blocks'),
    'file' => 'block.admin.inc',
  );
  $items['admin/structure/block/manage/%block'] = array(
    'title' => 'Configure block',
    'page callback' => 'block_admin_edit',
    'page arguments' => array(4),
136
    'access arguments' => array('administer blocks'),
137
    'file' => 'block.admin.inc',
138
  );
139
  $items['admin/structure/block/manage/%block/configure'] = array(
140
141
142
143
    'title' => 'Configure block',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
  );
144
  $items['admin/structure/block/manage/%block/delete'] = array(
145
    'title' => 'Delete block',
146
147
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_NONE,
148
    'route_name' => 'block_admin_block_delete',
149
  );
150
151
152
153
154
155
  // Block administration is tied to the theme and plugin definition so
  // that the plugin can appropriately attach to this URL structure.
  // @todo D8: Use dynamic % arguments instead of static, hard-coded theme names
  //   and plugin IDs to decouple the routes from these dependencies and allow
  //   hook_menu_local_tasks() to check for the untranslated tab_parent path.
  // @see http://drupal.org/node/1067408
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  $themes = list_themes();
  foreach (drupal_container()->get('plugin.manager.system.plugin_ui')->getDefinitions() as $plugin_id => $plugin) {
    list($plugin_base, $key) = explode(':', $plugin_id);
    if ($plugin_base == 'block_plugin_ui') {
      $theme = $themes[$key];
      $items['admin/structure/block/list/' . $plugin_id] = array(
        'title' => check_plain($theme->info['name']),
        'page arguments' => array($key),
        'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
        'access callback' => '_block_themes_access',
        'access arguments' => array($key),
        'file' => 'block.admin.inc',
      );
      $items['admin/structure/block/demo/' . $key] = array(
        'title' => check_plain($theme->info['name']),
        'page callback' => 'block_admin_demo',
        'page arguments' => array($key),
        'type' => MENU_CALLBACK,
        'access callback' => '_block_themes_access',
        'access arguments' => array($key),
        'theme callback' => '_block_custom_theme',
        'theme arguments' => array($key),
178
179
180
        'file' => 'block.admin.inc',
      );
    }
Dries's avatar
   
Dries committed
181
  }
Dries's avatar
   
Dries committed
182
  return $items;
Dries's avatar
   
Dries committed
183
184
}

185
/**
186
 * Access callback: Only enabled themes can be accessed.
187
188
189
190
191
192
193
194
195
 *
 * Path:
 * - admin/structure/block/list/% (for each theme)
 * - admin/structure/block/demo/% (for each theme)
 *
 * @param $theme
 *   Either the name of a theme or a full theme object.
 *
 * @see block_menu()
196
197
 */
function _block_themes_access($theme) {
198
199
200
201
  return user_access('administer blocks') && drupal_theme_access($theme);
}

/**
202
203
 * Theme callback: Uses the theme specified in the parameter.
 *
204
205
206
 * @param $theme
 *   The theme whose blocks are being configured. If not set, the default theme
 *   is assumed.
207
 *
208
209
210
 * @return
 *   The theme that should be used for the block configuration page, or NULL
 *   to indicate that the default theme should be used.
211
212
 *
 * @see block_menu()
213
214
215
216
217
 */
function _block_custom_theme($theme = NULL) {
  // We return exactly what was passed in, to guarantee that the page will
  // always be displayed using the theme whose blocks are being configured.
  return $theme;
218
219
}

220
/**
221
 * Implements hook_page_build().
222
 *
223
 * Renders blocks into their regions.
224
 */
225
function block_page_build(&$page) {
226
227
228
  global $theme;

  // The theme system might not yet be initialized. We need $theme.
229
  drupal_theme_initialize();
230

231
  // Fetch a list of regions for the current theme.
232
  $all_regions = system_region_list($theme);
233

234
235
236
237
238
239
240
241
  $item = menu_get_item();
  if ($item['path'] != 'admin/structure/block/demo/' . $theme) {
    // Load all region content assigned via blocks.
    foreach (array_keys($all_regions) as $region) {
      // Assign blocks to region.
      if ($blocks = block_get_blocks_by_region($region)) {
        $page[$region] = $blocks;
      }
242
    }
243
244
245
246
247
248
249
250
251
    // Once we've finished attaching all blocks to the page, clear the static
    // cache to allow modules to alter the block list differently in different
    // contexts. For example, any code that triggers hook_page_build() more
    // than once in the same page request may need to alter the block list
    // differently each time, so that only certain parts of the page are
    // actually built. We do not clear the cache any earlier than this, though,
    // because it is used each time block_get_blocks_by_region() gets called
    // above.
    drupal_static_reset('block_list');
252
253
254
  }
  else {
    // Append region description if we are rendering the regions demo page.
255
    $item = menu_get_item();
256
257
258
    if ($item['path'] == 'admin/structure/block/demo/' . $theme) {
      $visible_regions = array_keys(system_region_list($theme, REGIONS_VISIBLE));
      foreach ($visible_regions as $region) {
259
260
261
262
263
        $description = '<div class="block-region">' . $all_regions[$region] . '</div>';
        $page[$region]['block_description'] = array(
          '#markup' => $description,
          '#weight' => 15,
        );
264
      }
265
266
267
      $page['page_top']['backlink'] = array(
        '#type' => 'link',
        '#title' => t('Exit block region demonstration'),
268
        '#href' => 'admin/structure/block' . (config('system.theme')->get('default') == $theme ? '' : '/list/' . $theme),
269
270
271
272
273
        // Add the "overlay-restore" class to indicate this link should restore
        // the context in which the region demonstration page was opened.
        '#options' => array('attributes' => array('class' => array('block-demo-backlink', 'overlay-restore'))),
        '#weight' => -10,
      );
274
275
276
277
278
    }
  }
}

/**
279
 * Gets a renderable array of a region containing all enabled blocks.
280
281
282
 *
 * @param $region
 *   The requested region.
283
284
285
 *
 * @return
 *   A renderable array of a region containing all enabled blocks.
286
287
288
289
 */
function block_get_blocks_by_region($region) {
  $build = array();
  if ($list = block_list($region)) {
290
    $build = _block_get_renderable_region($list);
291
292
293
294
295
  }
  return $build;
}

/**
296
 * Gets an array of blocks suitable for drupal_render().
297
298
299
 *
 * @param $list
 *   A list of blocks such as that returned by block_list().
300
 *
301
302
303
 * @return
 *   A renderable array.
 */
304
function _block_get_renderable_region($list = array()) {
305
  $build = array();
306
307
308
309
310
311
312
313
  // Block caching is not compatible with node_access modules. We also
  // preserve the submission of forms in blocks, by fetching from cache
  // only if the request method is 'GET' (or 'HEAD'). User 1 being out of
  // the regular 'roles define permissions' schema, it brings too many
  // chances of having unwanted output get in the cache and later be served
  // to other users. We therefore exclude user 1 from block caching.
  $not_cacheable = $GLOBALS['user']->uid == 1 ||
    count(module_implements('node_grants')) ||
314
    !\Drupal::request()->isMethodSafe();
315

316
  foreach ($list as $key => $block) {
317
318
319
320
321
322
    $settings = $block->get('settings');
    if ($not_cacheable || in_array($settings['cache'], array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM))) {
      // Non-cached blocks get built immediately.
      if ($block->access()) {
        $build[$key] = entity_view($block, 'block');
      }
323
324
    }
    else {
325
326
      $key_components = explode('.', $key);
      $id = array_pop($key_components);
327
328
329
      $build[$key] = array(
        '#block' => $block,
        '#weight' => $block->get('weight'),
330
331
        '#pre_render' => array('_block_get_renderable_block'),
        '#cache' => array(
332
          'keys' => array($id, $settings['module']),
333
          'granularity' => $settings['cache'],
334
          'bin' => 'block',
335
          'tags' => array('content' => TRUE),
336
337
338
        ),
      );
    }
339

340
341
342
343
344
    // Add contextual links for this block; skip the main content block, since
    // contextual links are basically output as tabs/local tasks already. Also
    // skip the help block, since we assume that most users do not need or want
    // to perform contextual actions on the help block, and the links needlessly
    // draw attention on it.
345
    if (isset($build[$key]) && !in_array($block->get('plugin'), array('system_help_block', 'system_main_block'))) {
346
      $build[$key]['#contextual_links']['block'] = array('admin/structure/block/manage', array($key));
347
348
349
350
351
352

      // If there are any nested contextual links, move them to the top level.
      if (isset($build[$key]['content']['#contextual_links'])) {
        $build[$key]['#contextual_links'] += $build[$key]['content']['#contextual_links'];
        unset($build[$key]['content']['#contextual_links']);
      }
353
    }
354
355
356
357
  }
  return $build;
}

Dries's avatar
   
Dries committed
358
/**
359
 * Returns an array of block class instances by theme.
Dries's avatar
   
Dries committed
360
 *
361
 * @param $theme
362
363
 *   The theme to rehash blocks for. If not provided, defaults to the currently
 *   used theme.
364
 *
Dries's avatar
   
Dries committed
365
 * @return
366
 *   Blocks currently exported by modules.
Dries's avatar
   
Dries committed
367
 */
368
function _block_rehash($theme = NULL) {
369
  $theme = $theme ? $theme : config('system.theme')->get('default');
370
  $regions = system_region_list($theme);
371
372
373
  $blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
  foreach ($blocks as $block_id => $block) {
    $region = $block->get('region');
374
    $status = $block->status();
375
    // Disable blocks in invalid regions.
376
    if (!empty($region) && $region != BLOCK_REGION_NONE && !isset($regions[$region]) && $status) {
377
      drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $block_id, '%region' => $region)), 'warning');
378
379
      // Disabled modules are moved into the BLOCK_REGION_NONE later so no
      // need to move the block to another region.
380
      $block->disable()->save();
381
    }
382
383
    // Set region to none if not enabled.
    if (!$status) {
384
385
      $block->set('region', BLOCK_REGION_NONE);
      $block->save();
386
    }
387
  }
Dries's avatar
   
Dries committed
388
389
  return $blocks;
}
Dries's avatar
   
Dries committed
390

391
/**
392
393
394
395
 * Initializes blocks for enabled themes.
 *
 * @param $theme_list
 *   An array of theme names.
396
 */
397
398
399
function block_themes_enabled($theme_list) {
  foreach ($theme_list as $theme) {
    block_theme_initialize($theme);
400
401
402
403
  }
}

/**
404
 * Assigns an initial, default set of blocks for a theme.
405
 *
406
407
408
409
 * This function is called the first time a new theme is enabled. The new theme
 * gets a copy of the default theme's blocks, with the difference that if a
 * particular region isn't available in the new theme, the block is assigned
 * to the new theme's default region.
410
 *
411
412
413
 * @param $theme
 *   The name of a theme.
 */
414
function block_theme_initialize($theme) {
415
  // Initialize theme's blocks if none already registered.
416
  $has_blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
417
  if (!$has_blocks) {
418
    $default_theme = config('system.theme')->get('default');
419
420
    // Apply only to new theme's visible regions.
    $regions = system_region_list($theme, REGIONS_VISIBLE);
421
422
423
424
425
    $default_theme_blocks = entity_load_multiple_by_properties('block', array('theme' => $default_theme));
    foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
      list(, $machine_name) = explode('.', $default_theme_block_id);
      $block = $default_theme_block->createDuplicate();
      $block->set('id', $theme . '.' . $machine_name);
426
427
      // If the region isn't supported by the theme, assign the block to the
      // theme's default region.
428
429
      if (!isset($regions[$block->get('region')])) {
        $block->set('region', system_default_region($theme));
430
      }
431
      $block->save();
432
433
434
435
    }
  }
}

436
/**
437
 * Returns all blocks in the specified region for the current user.
438
439
440
441
442
 *
 * @param $region
 *   The name of a region.
 *
 * @return
443
444
445
446
 *   An array of block objects, indexed with the configuration object name
 *   that represents the configuration. If you are displaying your blocks in
 *   one or two sidebars, you may check whether this array is empty to see
 *   how many columns are going to be displayed.
447
448
 */
function block_list($region) {
449
  $blocks = &drupal_static(__FUNCTION__);
450

451
  if (!isset($blocks)) {
452
453
454
455
456
    global $theme;
    $blocks = array();
    foreach (entity_load_multiple_by_properties('block', array('theme' => $theme)) as $block_id => $block) {
      $blocks[$block->get('region')][$block_id] = $block;
    }
457
458
  }

459
  // Create an empty array if there are no entries.
460
461
462
463
464
465
466
  if (!isset($blocks[$region])) {
    $blocks[$region] = array();
  }

  return $blocks[$region];
}

467
/**
468
 * Loads a block instance.
469
 *
470
 * This should only be used when entity_load() cannot be used directly.
471
 *
472
473
 * @param string $entity_id
 *   The block ID.
474
 *
475
476
 * @return \Drupal\block\Plugin\Core\Entity\Block
 *   The loaded block object.
477
 */
478
479
function block_load($entity_id) {
  return entity_load('block', $entity_id);
480
481
}

482
/**
483
 * Builds the content and label for a block.
484
485
 *
 * For cacheable blocks, this is called during #pre_render.
486
 *
487
488
 * @param $element
 *   A renderable array.
489
 *
490
 * @return
491
 *   A renderable array.
492
 */
493
494
function _block_get_renderable_block($element) {
  $block = $element['#block'];
495
496
  // Don't bother to build blocks that aren't accessible.
  if ($element['#access'] = $block->access()) {
497
    $element += entity_view($block, 'block');
498
  }
499
  return $element;
500
}
501

502
503
504
505
/**
 * Implements hook_rebuild().
 */
function block_rebuild() {
506
507
508
509
  foreach (list_themes() as $name => $data) {
    if ($data->status) {
      _block_rehash($name);
    }
510
  }
511
}
512
513

/**
514
515
516
 * Prepares variables for block templates.
 *
 * Default template: block.html.twig.
517
 *
518
 * Prepares the values passed to the theme_block function to be passed
519
520
 * into a pluggable template engine. Uses block properties to generate a
 * series of template file suggestions. If none are found, the default
521
 * block.html.twig is used.
522
 *
523
524
525
 * Most themes use their own copy of block.html.twig. The default is located
 * inside "core/modules/block/templates/block.html.twig". Look in there for the
 * full list of available variables.
526
 *
527
528
529
530
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the properties of the element.
 *     Properties used: #block, #configuration, #children, #plugin_id.
531
532
 */
function template_preprocess_block(&$variables) {
533
  $block_counter = &drupal_static(__FUNCTION__, array());
534

535
536
537
538
  $variables['configuration'] = $variables['elements']['#configuration'];
  $variables['plugin_id'] = $variables['elements']['#plugin_id'];
  $variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
  $variables['content'] = $variables['elements']['content'];
539

540
  $variables['attributes']['class'][] = 'block';
541
  $variables['attributes']['class'][] = drupal_html_class('block-' . $variables['configuration']['module']);
542

543
544
545
546
547
548
549
550
551
552
  // The block template provides a wrapping element for the content. Render the
  // #attributes of the content on this wrapping element rather than passing
  // them through to the content's #theme function/template. This allows the
  // content to not require a function/template at all, or if it does use one,
  // to not require it to output an extra wrapping element.
  if (isset($variables['content']['#attributes'])) {
    $variables['content_attributes'] = NestedArray::mergeDeep($variables['content_attributes'], $variables['content']['#attributes']);
    unset($variables['content']['#attributes']);
  }

553
  // Add default class for block content.
554
  $variables['content_attributes']['class'][] = 'content';
555

556
  $variables['theme_hook_suggestions'][] = 'block__' . $variables['configuration']['module'];
557
558
559
  // Hyphens (-) and underscores (_) play a special role in theme suggestions.
  // Theme suggestions should only contain underscores, because within
  // drupal_find_theme_templates(), underscores are converted to hyphens to
560
  // match template file names, and then converted back to underscores to match
561
562
563
564
  // pre-processing and other function names. So if your theme suggestion
  // contains a hyphen, it will end up as an underscore after this conversion,
  // and your function names won't be recognized. So, we need to convert
  // hyphens to underscores in block deltas for the theme suggestions.
565

566
567
  // We can safely explode on : because we know the Block plugin type manager
  // enforces that delimiter for all derivatives.
568
  $parts = explode(':', $variables['plugin_id']);
569
570
571
572
  $suggestion = 'block';
  while ($part = array_shift($parts)) {
    $variables['theme_hook_suggestions'][] = $suggestion .= '__' . strtr($part, '-', '_');
  }
573
  // Create a valid HTML ID and make sure it is unique.
574
575
  if ($id = $variables['elements']['#block']->id()) {
    $config_id = explode('.', $id);
576
    $machine_name = array_pop($config_id);
577
    $variables['attributes']['id'] = drupal_html_id('block-' . $machine_name);
578
579
    $variables['theme_hook_suggestions'][] = 'block__' . $machine_name;
  }
580
}
581

582
/**
583
 * Implements hook_user_role_delete().
584
 *
585
 * Removes deleted role from blocks that use it.
586
587
 */
function block_user_role_delete($role) {
588
589
  foreach (entity_load_multiple('block') as $block_id => $block) {
    $visibility = $block->get('visibility');
590
591
    if (isset($visibility['roles']['roles'][$role->id()])) {
      unset($visibility['roles']['roles'][$role->id()]);
592
593
      $block->set('visibility', $visibility);
      $block->save();
594
595
    }
  }
596
597
}

598
/**
599
 * Implements hook_menu_delete().
600
601
 */
function block_menu_delete($menu) {
602
603
604
  foreach (entity_load_multiple('block') as $block_id => $block) {
    if ($block->get('plugin') == 'menu_menu_block:' . $menu->id()) {
      $block->delete();
605
606
    }
  }
607
608
}

609
610
611
612
613
614
615
616
617
618
619
620
/**
 * Implements hook_admin_paths().
 */
function block_admin_paths() {
  $paths = array(
    // Exclude the block demonstration page from admin (overlay) treatment.
    // This allows us to present this page in its true form, full page.
    'admin/structure/block/demo/*' => FALSE,
  );
  return $paths;
}

621
622
623
624
625
626
627
/**
 * Implements hook_language_delete().
 *
 * Delete the potential block visibility settings of the deleted language.
 */
function block_language_delete($language) {
  // Remove the block visibility settings for the deleted language.
628
629
630
631
632
633
  foreach (entity_load_multiple('block') as $block_id => $block) {
    $visibility = $block->get('visibility');
    if (isset($visibility['language']['langcodes'][$language->langcode])) {
      unset($visibility['language']['langcodes'][$language->langcode]);
      $block->set('visibility', $visibility);
      $block->save();
634
635
    }
  }
636
}
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655

/**
 * Implements hook_library_info().
 */
function block_library_info() {
  $libraries['drupal.block'] = array(
    'title' => 'Block',
    'version' => VERSION,
    'js' => array(
      drupal_get_path('module', 'block') . '/block.js' => array(),
    ),
    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'drupal'),
    ),
  );

  return $libraries;
}