summaryrefslogtreecommitdiffstats
path: root/core/modules/block/block.admin.inc
blob: 6af9fe398314467984da283e5907ecfa9f8ad42b (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
<?php

/**
 * @file
 * Admin page callbacks for the block module.
 */

use Drupal\block\Plugin\Core\Entity\Block;
use Drupal\Core\Template\Attribute;

/**
 * Page callback: Attaches CSS for the block region demo.
 *
 * @see block_menu()
 */
function block_admin_demo($theme = NULL) {
  drupal_add_css(drupal_get_path('module', 'block') . '/block.admin.css');
  return '';
}

/**
 * Page callback: Shows the block administration page.
 *
 * @param string $theme
 *   The theme to display the administration page for.
 *
 * @return array
 *   A render array for a page containing a list of blocks.
 *
 * @see block_menu()
 */
function block_admin_display($theme) {
  return Drupal::entityManager()
    ->getListController('block')
    ->render($theme);
}

/**
 * Page callback: Build the block instance add form.
 *
 * @param string $plugin_id
 *   The plugin ID for the block instance.
 * @param string $theme
 *   The name of the theme for the block instance.
 *
 * @return array
 *   The block instance edit form.
 */
function block_admin_add($plugin_id, $theme) {
  $entity = entity_create('block', array(
    'plugin' => $plugin_id,
    'theme' => $theme,
  ));
  return entity_get_form($entity);
}

/**
 * Page callback: Build the block instance edit form.
 *
 * @param \Drupal\block\Plugin\Core\Entity\Block $entity
 *   The block instance.
 *
 * @return array
 *   The block instance edit form.
 */
function block_admin_edit(Block $entity) {
  // Get the theme for the page title.
  $admin_theme = config('system.theme')->get('admin');
  $themes = list_themes();
  $theme_key = $entity->get('theme');
  $theme = $themes[$theme_key];
  // Use meaningful titles for the main site and administrative themes.
  $theme_title = $theme->info['name'];
  if ($theme_key == config('system.theme')->get('default')) {
    $theme_title = t('!theme (default theme)', array('!theme' => $theme_title));
  }
  elseif ($admin_theme && $theme_key == $admin_theme) {
    $theme_title = t('!theme (administration theme)', array('!theme' => $theme_title));
  }

  // Get the block label for the page title.
  drupal_set_title(t("Configure %label block in %theme", array('%label' => $entity->label(), '%theme' => $theme_title)), PASS_THROUGH);

  return entity_get_form($entity);
}

/**
 * Prepares variables for block admin display form templates.
 *
 * Default template: block-admin-display-form.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 */
function template_preprocess_block_admin_display_form(&$variables) {
  $variables['block_regions'] = $variables['form']['block_regions']['#value'];
  if (isset($variables['block_regions'][BLOCK_REGION_NONE])) {
    $variables['block_regions'][BLOCK_REGION_NONE] = t('Disabled');
  }

  foreach ($variables['block_regions'] as $key => $value) {
    // Initialize an empty array for the region.
    $variables['block_listing'][$key] = array();
  }

  $default_attributes = new Attribute(array('class' => array('draggable')));
  $row = 0;

  // Initialize disabled blocks array.
  $variables['block_listing'][BLOCK_REGION_NONE] = array();

  // Add each block in the form to the appropriate place in the block listing.
  foreach (element_children($variables['form']['blocks']) as $i) {
    $block = &$variables['form']['blocks'][$i];

    // Fetch the region for the current block.
    $region = (isset($block['region']['#default_value']) ? $block['region']['#default_value'] : BLOCK_REGION_NONE);

    // Set special classes needed for table drag and drop.
    $block['region']['#attributes']['class'] = array('block-region-select', 'block-region-' . $region);
    $block['weight']['#attributes']['class'] = array('block-weight', 'block-weight-' . $region);

    $variables['block_listing'][$region][$i] = new stdClass();
    $variables['block_listing'][$region][$i]->attributes = clone $default_attributes;
    $variables['block_listing'][$region][$i]->attributes['class'][] = $row % 2 == 0 ? 'odd' : 'even';
    $row++;
    if(!empty($block['#attributes']['class'])) {
      $variables['block_listing'][$region][$i]->attributes['class'][] = implode(' ', $block['#attributes']['class']);
    }

    $variables['block_listing'][$region][$i]->row_class = !empty($block['#attributes']['class']) ? implode(' ', $block['#attributes']['class']) : '';
    $variables['block_listing'][$region][$i]->block_modified = !empty($block['#attributes']['class']) && in_array('block-modified', $block['#attributes']['class']);
    $variables['block_listing'][$region][$i]->block_title = $block['info'];
    $variables['block_listing'][$region][$i]->region_select = array(
      'region' => array(
        'data' => $block['region'],
        '#weight' => 1,
      ),
      'theme' => array(
        'data' => $block['theme'],
        '#weight' => 2,
      ),
    );
    $variables['block_listing'][$region][$i]->weight_select = $block['weight'];
    $variables['block_listing'][$region][$i]->operations = $block['operations'];
    $variables['block_listing'][$region][$i]->printed = FALSE;
  }
}