public BlockForm::form(array $form, FormStateInterface $form_state)
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- core/modules/block/src/BlockForm.php, line 123
Class
- BlockForm
- Provides form for block instance forms.
Namespace
Drupal\block
Code
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 | public function form( array $form , FormStateInterface $form_state ) { $entity = $this ->entity; // Store theme settings in $form_state for use below. if (! $theme = $entity ->getTheme()) { $theme = $this ->config( 'system.theme' )->get( 'default' ); } $form_state ->set( 'block_theme' , $theme ); // Store the gathered contexts in the form state for other objects to use // during form building. $form_state ->setTemporaryValue( 'gathered_contexts' , $this ->contextRepository->getAvailableContexts()); $form [ '#tree' ] = TRUE; $form [ 'settings' ] = []; $subform_state = SubformState::createForSubform( $form [ 'settings' ], $form , $form_state ); $form [ 'settings' ] = $this ->getPluginForm( $entity ->getPlugin())->buildConfigurationForm( $form [ 'settings' ], $subform_state ); $form [ 'visibility' ] = $this ->buildVisibilityInterface([], $form_state ); // If creating a new block, calculate a safe default machine name. $form [ 'id' ] = array ( '#type' => 'machine_name' , '#maxlength' => 64, '#description' => $this ->t( 'A unique name for this block instance. Must be alpha-numeric and underscore separated.' ), '#default_value' => ! $entity ->isNew() ? $entity ->id() : $this ->getUniqueMachineName( $entity ), '#machine_name' => array ( 'exists' => '\Drupal\block\Entity\Block::load' , 'replace_pattern' => '[^a-z0-9_.]+' , 'source' => array ( 'settings' , 'label' ), ), '#required' => TRUE, '#disabled' => ! $entity ->isNew(), ); // Theme settings. if ( $entity ->getTheme()) { $form [ 'theme' ] = array ( '#type' => 'value' , '#value' => $theme , ); } else { $theme_options = array (); foreach ( $this ->themeHandler->listInfo() as $theme_name => $theme_info ) { if (! empty ( $theme_info ->status)) { $theme_options [ $theme_name ] = $theme_info ->info[ 'name' ]; } } $form [ 'theme' ] = array ( '#type' => 'select' , '#options' => $theme_options , '#title' => t( 'Theme' ), '#default_value' => $theme , '#ajax' => array ( 'callback' => '::themeSwitch' , 'wrapper' => 'edit-block-region-wrapper' , ), ); } // Hidden weight setting. $weight = $entity ->isNew() ? $this ->getRequest()->query->get( 'weight' , 0) : $entity ->getWeight(); $form [ 'weight' ] = array ( '#type' => 'hidden' , '#default_value' => $weight , ); // Region settings. $entity_region = $entity ->getRegion(); $region = $entity ->isNew() ? $this ->getRequest()->query->get( 'region' , $entity_region ) : $entity_region ; $form [ 'region' ] = array ( '#type' => 'select' , '#title' => $this ->t( 'Region' ), '#description' => $this ->t( 'Select the region where this block should be displayed.' ), '#default_value' => $region , '#empty_value' => BlockInterface::BLOCK_REGION_NONE, '#options' => system_region_list( $theme , REGIONS_VISIBLE), '#prefix' => '<div id="edit-block-region-wrapper">' , '#suffix' => '</div>' , ); $form [ '#attached' ][ 'library' ][] = 'block/drupal.block.admin' ; return $form ; } |
Please login to continue.