/** * @covers ::getRegionNames */ public function testGetRegionNames() { $region_names = ['Foo', 'Bar', 'Baz']; $this->layout->getPluginDefinition()->willReturn(['region_names' => $region_names]); $this->assertSame($region_names, $this->variant->getRegionNames()); }
/** * Builds a form that constructs a unsaved instance of a Block for the IPE. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param string $plugin_id * The requested Block Plugin ID. * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display * The current PageVariant ID. * @param string $uuid * An optional Block UUID, if this is an existing Block. * * @return array * The form structure. */ public function buildForm(array $form, FormStateInterface $form_state, $plugin_id = NULL, PanelsDisplayVariant $panels_display = NULL, $uuid = NULL) { // We require these default arguments. if (!$plugin_id || !$panels_display) { return FALSE; } // Save the panels display for later. $this->panelsDisplay = $panels_display; // Grab the current layout's regions. $regions = $panels_display->getRegionNames(); // If $uuid is present, a block should exist. if ($uuid) { /** @var \Drupal\Core\Block\BlockBase $block_instance */ $block_instance = $panels_display->getBlock($uuid); } else { // Create an instance of this Block plugin. /** @var \Drupal\Core\Block\BlockBase $block_instance */ $block_instance = $this->blockManager->createInstance($plugin_id); } // Determine the current region. $block_config = $block_instance->getConfiguration(); if (isset($block_config['region']) && isset($regions[$block_config['region']])) { $region = $block_config['region']; } else { $region = reset($regions); } // Wrap the form so that our AJAX submit can replace its contents. $form['#prefix'] = '<div id="panels-ipe-block-plugin-form-wrapper">'; $form['#suffix'] = '</div>'; // Add our various card wrappers. $form['flipper'] = ['#type' => 'container', '#attributes' => ['class' => 'flipper']]; $form['flipper']['front'] = ['#type' => 'container', '#attributes' => ['class' => 'front']]; $form['flipper']['back'] = ['#type' => 'container', '#attributes' => ['class' => 'back']]; $form['#attributes']['class'][] = 'flip-container'; // Get the base configuration form for this block. $form['flipper']['front']['settings'] = $block_instance->buildConfigurationForm([], $form_state); $form['flipper']['front']['settings']['context_mapping'] = $this->addContextAssignmentElement($block_instance, $this->panelsDisplay->getContexts()); $form['flipper']['front']['settings']['#tree'] = TRUE; // Add the block ID, variant ID to the form as values. $form['plugin_id'] = ['#type' => 'value', '#value' => $plugin_id]; $form['variant_id'] = ['#type' => 'value', '#value' => $panels_display->id()]; $form['uuid'] = ['#type' => 'value', '#value' => $uuid]; // Add a select list for region assignment. $form['flipper']['front']['settings']['region'] = ['#title' => $this->t('Region'), '#type' => 'select', '#options' => $regions, '#required' => TRUE, '#default_value' => $region]; // Add an add button, which is only used by our App. $form['submit'] = ['#type' => 'button', '#value' => $uuid ? $this->t('Update') : $this->t('Add'), '#ajax' => ['callback' => '::submitForm', 'wrapper' => 'panels-ipe-block-plugin-form-wrapper', 'method' => 'replace', 'progress' => ['type' => 'throbber', 'message' => '']]]; // Add a preview button. $form['preview'] = ['#type' => 'button', '#value' => $this->t('Toggle Preview'), '#ajax' => ['callback' => '::submitPreview', 'wrapper' => 'panels-ipe-block-plugin-form-wrapper', 'method' => 'replace', 'progress' => ['type' => 'throbber', 'message' => '']]]; return $form; }