/** * Tests the block config schema for block plugins. */ public function testBlockConfigSchema() { foreach ($this->blockManager->getDefinitions() as $block_id => $definition) { $id = strtolower($this->randomName()); $block = Block::create(array('id' => $id, 'theme' => 'stark', 'weight' => 00, 'status' => TRUE, 'region' => 'content', 'plugin' => $block_id, 'settings' => array('label' => $this->randomName(), 'provider' => 'system', 'label_display' => FALSE), 'visibility' => array())); $block->save(); $config = \Drupal::config("block.block.{$id}"); $this->assertEqual($config->get('id'), $id); $this->assertConfigSchema($this->typedConfig, $config->getName(), $config->get()); } }
/** * Retrieves suggestions for block category autocompletion. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request. * * @return \Symfony\Component\HttpFoundation\JsonResponse * A JSON response containing autocomplete suggestions. */ public function autocomplete(Request $request) { $typed_category = $request->query->get('q'); $matches = array(); foreach ($this->blockManager->getCategories() as $category) { if (stripos($category, $typed_category) === 0) { $matches[] = array('value' => $category, 'label' => String::checkPlain($category)); } } return new JsonResponse($matches); }
/** * Presents a list of blocks to add to the page variant. * * @param \Drupal\page_manager\PageInterface $layout * The page entity. * @param string $layout_region_id * The page variant ID. * * @return array * The block selection page. */ public function selectBlock(PageInterface $page, $page_variant_id, $layout_region_id = NULL) { // Add a section containing the available blocks to be added to the variant. $form = array('#type' => 'container', '#attached' => array('library' => array('core/drupal.ajax'))); // Sort the plugins first by category, then by label. $plugins = $this->blockManager->getDefinitionsForContexts($page->getContexts()); foreach ($plugins as $plugin_id => $plugin_definition) { $category = String::checkPlain($plugin_definition['category']); $category_key = 'category-' . $category; if (!isset($form['place_blocks']['list'][$category_key])) { $form['place_blocks']['list'][$category_key] = array('#type' => 'details', '#title' => $category, '#open' => TRUE, 'content' => array('#theme' => 'links', '#links' => array(), '#attributes' => array('class' => array('block-list')))); } $form['place_blocks']['list'][$category_key]['content']['#links'][$plugin_id] = array('title' => $plugin_definition['admin_label'], 'href' => '/admin/structure/page_manager/manage/' . $page->id() . '/manage/' . $page_variant_id . '/layout/' . $layout_region_id . '/block/' . $plugin_id . '/add', 'attributes' => array('class' => array('use-ajax', 'block-filter-text-source'), 'data-accepts' => 'application/vnd.drupal-modal', 'data-dialog-options' => Json::encode(array('width' => 700)))); } return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { $configurable_types = $form['#language_types']; $stored_values = $this->languageTypes->get('configurable'); $customized = array(); $method_weights_type = array(); foreach ($configurable_types as $type) { $customized[$type] = in_array($type, $stored_values); $method_weights = array(); $enabled_methods = $form_state['values'][$type]['enabled']; $enabled_methods[LanguageNegotiationSelected::METHOD_ID] = TRUE; $method_weights_input = $form_state['values'][$type]['weight']; if (isset($form_state['values'][$type]['configurable'])) { $customized[$type] = !empty($form_state['values'][$type]['configurable']); } foreach ($method_weights_input as $method_id => $weight) { if ($enabled_methods[$method_id]) { $method_weights[$method_id] = $weight; } } $method_weights_type[$type] = $method_weights; $this->config('language.types')->set('negotiation.' . $type . '.method_weights', $method_weights_input)->save(); } // Update non-configurable language types and the related language // negotiation configuration. $this->negotiator->updateConfiguration(array_keys(array_filter($customized))); // Update the language negotiations after setting the configurability. foreach ($method_weights_type as $type => $method_weights) { $this->negotiator->saveConfiguration($type, $method_weights); } // Clear block definitions cache since the available blocks and their names // may have been changed based on the configurable types. if ($this->blockManager) { // If there is an active language switcher for a language type that has // been made not configurable, deactivate it first. $non_configurable = array_keys(array_diff($customized, array_filter($customized))); $this->disableLanguageSwitcher($non_configurable); $this->blockManager->clearCachedDefinitions(); } $form_state['redirect_route']['route_name'] = 'language.negotiation'; drupal_set_message($this->t('Language negotiation configuration saved.')); }
/** * Implements \Drupal\Core\Form\FormInterface::buildForm(). * * Form constructor for the main block administration form. */ public function buildForm(array $form, FormStateInterface $form_state) { $placement = FALSE; if ($this->request->query->has('block-placement')) { $placement = $this->request->query->get('block-placement'); $form['#attached']['js'][] = array('type' => 'setting', 'data' => array('blockPlacement' => $placement)); } $entities = $this->load(); $form['#theme'] = array('block_list'); $form['#attached']['library'][] = 'core/drupal.tableheader'; $form['#attached']['library'][] = 'block/drupal.block'; $form['#attached']['library'][] = 'block/drupal.block.admin'; $form['#attributes']['class'][] = 'clearfix'; // Add a last region for disabled blocks. $block_regions_with_disabled = $this->regions + array(BlockInterface::BLOCK_REGION_NONE => BlockInterface::BLOCK_REGION_NONE); $form['block_regions'] = array('#type' => 'value', '#value' => $block_regions_with_disabled); // Weights range from -delta to +delta, so delta should be at least half // of the amount of blocks present. This makes sure all blocks in the same // region get an unique weight. $weight_delta = round(count($entities) / 2); // Build the form tree. $form['edited_theme'] = array('#type' => 'value', '#value' => $this->theme); $form['blocks'] = array('#type' => 'table', '#header' => array(t('Block'), t('Category'), t('Region'), t('Weight'), t('Operations')), '#attributes' => array('id' => 'blocks')); // Build blocks first for each region. foreach ($entities as $entity_id => $entity) { $definition = $entity->getPlugin()->getPluginDefinition(); $blocks[$entity->get('region')][$entity_id] = array('label' => $entity->label(), 'entity_id' => $entity_id, 'weight' => $entity->get('weight'), 'entity' => $entity, 'category' => $definition['category']); } // Loop over each region and build blocks. foreach ($block_regions_with_disabled as $region => $title) { $form['blocks']['#tabledrag'][] = array('action' => 'match', 'relationship' => 'sibling', 'group' => 'block-region-select', 'subgroup' => 'block-region-' . $region, 'hidden' => FALSE); $form['blocks']['#tabledrag'][] = array('action' => 'order', 'relationship' => 'sibling', 'group' => 'block-weight', 'subgroup' => 'block-weight-' . $region); $form['blocks'][$region] = array('#attributes' => array('class' => array('region-title', 'region-title-' . $region), 'no_striping' => TRUE)); $form['blocks'][$region]['title'] = array('#markup' => $region != BlockInterface::BLOCK_REGION_NONE ? $title : t('Disabled'), '#wrapper_attributes' => array('colspan' => 5)); $form['blocks'][$region . '-message'] = array('#attributes' => array('class' => array('region-message', 'region-' . $region . '-message', empty($blocks[$region]) ? 'region-empty' : 'region-populated'))); $form['blocks'][$region . '-message']['message'] = array('#markup' => '<em>' . t('No blocks in this region') . '</em>', '#wrapper_attributes' => array('colspan' => 5)); if (isset($blocks[$region])) { foreach ($blocks[$region] as $info) { $entity_id = $info['entity_id']; $form['blocks'][$entity_id] = array('#attributes' => array('class' => array('draggable'))); if ($placement && $placement == drupal_html_class($entity_id)) { $form['blocks'][$entity_id]['#attributes']['id'] = 'block-placed'; } $form['blocks'][$entity_id]['info'] = array('#markup' => String::checkPlain($info['label']), '#wrapper_attributes' => array('class' => array('block'))); $form['blocks'][$entity_id]['type'] = array('#markup' => $info['category']); $form['blocks'][$entity_id]['region-theme']['region'] = array('#type' => 'select', '#default_value' => $region, '#empty_value' => BlockInterface::BLOCK_REGION_NONE, '#title' => t('Region for @block block', array('@block' => $info['label'])), '#title_display' => 'invisible', '#options' => $this->regions, '#attributes' => array('class' => array('block-region-select', 'block-region-' . $region)), '#parents' => array('blocks', $entity_id, 'region')); $form['blocks'][$entity_id]['region-theme']['theme'] = array('#type' => 'hidden', '#value' => $this->theme, '#parents' => array('blocks', $entity_id, 'theme')); $form['blocks'][$entity_id]['weight'] = array('#type' => 'weight', '#default_value' => $info['weight'], '#delta' => $weight_delta, '#title' => t('Weight for @block block', array('@block' => $info['label'])), '#title_display' => 'invisible', '#attributes' => array('class' => array('block-weight', 'block-weight-' . $region))); $form['blocks'][$entity_id]['operations'] = $this->buildOperations($info['entity']); } } } // Do not allow disabling the main system content block when it is present. if (isset($form['blocks']['system_main']['region'])) { $form['blocks']['system_main']['region']['#required'] = TRUE; } $form['actions'] = array('#tree' => FALSE, '#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save blocks'), '#button_type' => 'primary'); $form['place_blocks']['title'] = array('#type' => 'container', '#children' => '<h3>' . t('Place blocks') . '</h3>', '#attributes' => array('class' => array('entity-meta-header'))); $form['place_blocks']['filter'] = array('#type' => 'search', '#title' => t('Filter'), '#title_display' => 'invisible', '#size' => 30, '#placeholder' => t('Filter by block name'), '#attributes' => array('class' => array('block-filter-text'), 'data-element' => '.entity-meta', 'title' => t('Enter a part of the block name to filter by.'))); $form['place_blocks']['list']['#type'] = 'container'; $form['place_blocks']['list']['#attributes']['class'][] = 'entity-meta'; // Sort the plugins first by category, then by label. $plugins = $this->blockManager->getSortedDefinitions(); foreach ($plugins as $plugin_id => $plugin_definition) { $category = String::checkPlain($plugin_definition['category']); $category_key = 'category-' . $category; if (!isset($form['place_blocks']['list'][$category_key])) { $form['place_blocks']['list'][$category_key] = array('#type' => 'details', '#title' => $category, '#open' => TRUE, 'content' => array('#theme' => 'links', '#links' => array(), '#attributes' => array('class' => array('block-list')))); } $form['place_blocks']['list'][$category_key]['content']['#links'][$plugin_id] = array('title' => $plugin_definition['admin_label'], 'route_name' => 'block.admin_add', 'route_parameters' => array('plugin_id' => $plugin_id, 'theme' => $this->theme), 'attributes' => array('class' => array('use-ajax', 'block-filter-text-source'), 'data-accepts' => 'application/vnd.drupal-modal', 'data-dialog-options' => Json::encode(array('width' => 700)))); } return $form; }