You are here

class RenderExampleDemoForm in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 render_example/src/Form/RenderExampleDemoForm.php \Drupal\render_example\Form\RenderExampleDemoForm

Provides the form for toggling module features on and off.

Hierarchy

Expanded class hierarchy of RenderExampleDemoForm

Related topics

1 string reference to 'RenderExampleDemoForm'
render_example.routing.yml in modules/render_example/render_example.routing.yml
modules/render_example/render_example.routing.yml

File

modules/render_example/src/Form/RenderExampleDemoForm.php, line 16

Namespace

Drupal\render_example\Form
View source
class RenderExampleDemoForm extends ConfigFormBase {

  /**
   * Module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'render_example_demo_form';
  }

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
    parent::__construct($config_factory);
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('render_example.settings');
    $form['description'] = [
      '#markup' => $this
        ->t('This example shows what render arrays look like in the building of a page. It will not work unless the user running it has the "access devel information" privilege. It shows both the actual arrays used to build a page or block, and examples of altering the page late in its lifecycle.'),
    ];
    $form['show_arrays'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Show render arrays'),
      'render_example_show_block' => [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Show block render arrays'),
        '#default_value' => $config
          ->get('show_block'),
        // Only enable this option if the Devel module is enabled.
        '#access' => $this->moduleHandler
          ->moduleExists('devel'),
      ],
      'render_example_show_page' => [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Show page render arrays'),
        '#default_value' => $config
          ->get('show_page'),
        // Only enable this option if the Devel module is enabled.
        '#access' => $this->moduleHandler
          ->moduleExists('devel'),
      ],
      'render_example_devel' => [
        '#markup' => $this
          ->t('Install the Devel module (https://www.drupal.org/project/devel) to enable additional demonstration features.'),
        // Only display this if the Devel module is not already installed.
        '#access' => !$this->moduleHandler
          ->moduleExists('devel'),
      ],
    ];
    $form['page_fiddling'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Make changes on all pages via hook_preprocess_page()'),
      '#description' => $this
        ->t('Theses changes are all made via the function render_example_preprocess_page()'),
      'render_example_move_breadcrumbs' => [
        '#title' => $this
          ->t('Move the breadcrumbs to the top of the content area'),
        '#description' => $this
          ->t('Uses hook_preprocess_page() to move the breadcrumbs into another region.'),
        '#type' => 'checkbox',
        '#default_value' => $config
          ->get('move_breadcrumbs'),
      ],
      'render_example_reverse_sidebar' => [
        '#title' => $this
          ->t('Reverse ordering of sidebar_first elements (if it exists)'),
        '#description' => $this
          ->t('Uses hook_preprocess_page() to reverse the ordering of items in sidebar_first'),
        '#type' => 'checkbox',
        '#default_value' => $config
          ->get('reverse_sidebar'),
      ],
      'render_example_wrap_blocks' => [
        '#title' => $this
          ->t('Use #prefix and #suffix to wrap a div around every block'),
        '#description' => $this
          ->t('Uses hook_block_view_alter() to wrap all blocks with a div using #prefix and #suffix'),
        '#type' => 'checkbox',
        '#default_value' => $config
          ->get('wrap_blocks'),
      ],
    ];
    $form['tabledrag'] = [
      '#type' => 'table',
      '#id' => 'draggable-table',
      '#caption' => $this
        ->t('Our favorite colors.'),
      '#header' => [
        $this
          ->t('Name'),
        $this
          ->t('Favorite color'),
        $this
          ->t('Weight'),
      ],
      // #tabledrag and be used on #table elements in the context of a form.
      // When enabled, the table will be rendered with a drag & drop interface
      // that can be used to re-order elements within the table. Any changes you
      // make to the order will be made available to your validation and submit
      // handlers via values in $form_state->getValues().
      //
      // The #tabledrag property contains an array of options passed to the
      // drupal_attach_tabledrag() function. These options are used to generate
      // the necessary JavaScript settings to configure the tableDrag behavior
      // for this table.
      //
      // For more in-depth documentation of the options below see
      // drupal_attach_tabledrag().
      '#tabledrag' => [
        [
          // The HTML ID of the table to make draggable. See #id above.
          'table_id' => 'draggable-table',
          // The action to be done on the form item. Either 'match' 'depth', or
          // 'order'.
          'action' => 'order',
          // String describing where the "action" option should be performed.
          // Either 'parent', 'sibling', 'group', or 'self'.
          'relationship' => 'sibling',
          // Class name applied on all related form elements for this action.
          // See below.
          'group' => 'table-order-weight',
        ],
      ],
      // Rather than defining the values to insert into the table using the
      // #rows property you can define each row as a sub element of the table
      // render array. And each column in the row as a sub element of the row
      // array.
      [
        // Apply the 'draggable' class to each row in the table that you want to
        // be made draggable.
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        // The first two columsn are render arrays that display a string of
        // text.
        'name' => [
          '#plain_text' => $this
            ->t('Amber'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('teal'),
        ],
        // The third column is a #weight form field.
        'weight' => [
          '#type' => 'weight',
          '#title_display' => 'invisible',
          // Set the default value to whatever the current weight, or order, of
          // the element that this row represents is.
          '#default_value' => 1,
          // Set a class on each field that represents the value to manipulate
          // when the table is reordered. The name of this class should match
          // the value used for the 'group' argument in the #tabledrag property
          // above.
          '#attributes' => [
            'class' => [
              'table-order-weight',
            ],
          ],
        ],
      ],
      // The rest of this array is additional rows so there is some data in the
      // table to drag around.
      [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#plain_text' => $this
            ->t('Addi'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('green'),
        ],
        'weight' => [
          '#type' => 'weight',
          '#title_display' => 'invisible',
          '#default_value' => 2,
          '#attributes' => [
            'class' => [
              'table-order-weight',
            ],
          ],
        ],
      ],
      [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#plain_text' => $this
            ->t('Blake'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('#063'),
        ],
        'weight' => [
          '#type' => 'weight',
          '#title_display' => 'invisible',
          '#default_value' => 3,
          '#attributes' => [
            'class' => [
              'table-order-weight',
            ],
          ],
        ],
      ],
      [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#plain_text' => $this
            ->t('Enid'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('indigo'),
        ],
        'weight' => [
          '#type' => 'weight',
          '#title_display' => 'invisible',
          '#default_value' => 4,
          '#attributes' => [
            'class' => [
              'table-order-weight',
            ],
          ],
        ],
      ],
      [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#plain_text' => $this
            ->t('Joe'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('green'),
        ],
        'weight' => [
          '#type' => 'weight',
          '#title_display' => 'invisible',
          '#default_value' => 5,
          '#attributes' => [
            'class' => [
              'table-order-weight',
            ],
          ],
        ],
      ],
    ];
    $form['tableselect'] = [
      '#type' => 'table',
      '#caption' => $this
        ->t('Our favorite colors.'),
      '#header' => [
        $this
          ->t('Name'),
        $this
          ->t('Favorite color'),
      ],
      '#tableselect' => TRUE,
      // Rather than defining the values to insert into the table using the
      // #rows property you can define each row as a sub element of the table
      // render array. And each column in the row as a sub element of the row
      // array.
      [
        'name' => [
          '#plain_text' => $this
            ->t('Amber'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('teal'),
        ],
      ],
      // The rest of this array is additional rows so there is some data in the
      // table.
      [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#plain_text' => $this
            ->t('Addi'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('green'),
        ],
      ],
      [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#plain_text' => $this
            ->t('Blake'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('#063'),
        ],
      ],
      [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#plain_text' => $this
            ->t('Enid'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('indigo'),
        ],
      ],
      [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#plain_text' => $this
            ->t('Joe'),
        ],
        'color' => [
          '#plain_text' => $this
            ->t('green'),
        ],
      ],
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'render_example.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $config = $this
      ->config('render_example.settings');
    $config
      ->set('show_block', $values['render_example_show_block'])
      ->save();
    $config
      ->set('show_page', $values['render_example_show_page'])
      ->save();
    $config
      ->set('move_breadcrumbs', $values['render_example_move_breadcrumbs'])
      ->save();
    $config
      ->set('reverse_sidebar', $values['render_example_reverse_sidebar'])
      ->save();
    $config
      ->set('wrap_blocks', $values['render_example_wrap_blocks'])
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
RenderExampleDemoForm::$moduleHandler protected property Module handler service.
RenderExampleDemoForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
RenderExampleDemoForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
RenderExampleDemoForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
RenderExampleDemoForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RenderExampleDemoForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
RenderExampleDemoForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.