You are here

public function DashboardForm::form in Draggable dashboard 8.2

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/DashboardForm.php, line 57

Class

DashboardForm
Class DashboardForm.

Namespace

Drupal\draggable_dashboard\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\draggable_dashboard\Entity\DashboardEntityInterface $dashboard */
  $dashboard = $this->entity;
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $dashboard
      ->label(),
    '#description' => $this
      ->t('Label for the dashboard.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#maxlength' => 64,
    '#default_value' => $dashboard
      ->id(),
    '#machine_name' => [
      'exists' => [
        '\\Drupal\\draggable_dashboard\\Entity\\DashboardEntity',
        'load',
      ],
      'source' => [
        'title',
      ],
    ],
    '#disabled' => !$dashboard
      ->isNew(),
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $dashboard
      ->get('description'),
    '#description' => '',
  ];
  if ($dashboard
    ->isNew()) {
    $form['columns'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Dashboard columns'),
      '#options' => [
        1 => $this
          ->t('1 Column'),
        2 => $this
          ->t('2 Columns'),
        3 => $this
          ->t('3 Columns'),
        4 => $this
          ->t('4 Columns'),
      ],
      '#default_value' => $dashboard
        ->get('columns'),
    ];
  }
  if (!$dashboard
    ->isNew()) {
    $form['#attached']['library'][] = 'core/drupal.tableheader';
    $form['#attached']['library'][] = 'draggable_dashboard/main';
    $form['dashboard_blocks_table'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Block'),
        $this
          ->t('Region'),
        $this
          ->t('Weight'),
        [
          'data' => $this
            ->t('Operations'),
          'class' => [
            'text-align-right',
          ],
        ],
      ],
      '#empty' => $this
        ->t('No columns defined.'),
      '#attributes' => [
        'id' => 'dashboardblocks',
      ],
    ];

    // Prepare available regions list.
    $regionOptions = [];
    for ($i = 1; $i <= $dashboard
      ->get('columns'); $i++) {
      $regionOptions[$i] = $i . ' ' . $this
        ->t('Column');
    }
    $entities = $dashboard
      ->get('blocks');

    // 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);
    for ($i = 1; $i <= $dashboard
      ->get('columns'); $i++) {
      $region = $i;
      $title = $this
        ->t('Column #@id', [
        '@id' => $i,
      ]);
      $form['dashboard_blocks_table']['#tabledrag'][] = [
        'action' => 'match',
        'relationship' => 'sibling',
        'group' => 'block-region-select',
        'subgroup' => 'block-region-' . $region,
        'hidden' => FALSE,
      ];
      $form['dashboard_blocks_table']['#tabledrag'][] = [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'block-weight',
        'subgroup' => 'block-weight-' . $region,
      ];
      $form['dashboard_blocks_table']['region-' . $region] = [
        '#attributes' => [
          'class' => [
            'region-title',
            'region-title-' . $region,
          ],
          'no_striping' => TRUE,
        ],
      ];
      $form['dashboard_blocks_table']['region-' . $region]['title'] = [
        '#theme_wrappers' => [
          'container' => [
            '#attributes' => [
              'class' => 'region-title__action',
            ],
          ],
        ],
        '#prefix' => $title,
        '#type' => 'link',
        '#title' => $this
          ->t('Place block <span class="visually-hidden">in the %region region</span>', [
          '%region' => $title,
        ]),
        '#url' => Url::fromRoute('draggable_dashboard.block_library', [
          'dashboard_entity' => $dashboard
            ->id(),
          'region' => $region,
        ]),
        '#wrapper_attributes' => [
          'colspan' => 4,
        ],
        '#attributes' => [
          'class' => [
            'use-ajax',
            'button',
            'button--small',
            'place-blocks',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'width' => 700,
          ]),
        ],
      ];

      // Collect all blocks from one particular column.
      $relations = [];
      if (!empty($entities)) {
        foreach ($entities as $key => $entity) {
          if ($entity['column'] == $i) {
            $relations[$key] = $entity;
          }
        }
        uasort($relations, [
          $this,
          'sortBlocks',
        ]);
      }
      $form['dashboard_blocks_table']['region-' . $region . '-message'] = [
        '#attributes' => [
          'class' => [
            'region-message',
            'region-' . $region . '-message',
            empty($relations) ? 'region-empty' : 'region-populated',
          ],
        ],
      ];
      $form['dashboard_blocks_table']['region-' . $region . '-message']['message'] = [
        '#markup' => '<em>' . t('No blocks in this region') . '</em>',
        '#wrapper_attributes' => [
          'colspan' => 4,
        ],
      ];
      if (!empty($relations)) {
        foreach ($relations as $key => $relation) {

          /** @var \Drupal\Core\Block\BlockPluginInterface $block_instance */
          $block_instance = $this->blockPluginManager
            ->createInstance($relation['settings']['id'], $relation['settings']);
          $form['dashboard_blocks_table'][$key] = [
            '#attributes' => [
              'class' => [
                'draggable',
              ],
            ],
          ];
          $form['dashboard_blocks_table'][$key]['info'] = [
            '#plain_text' => $block_instance
              ->label(),
            '#wrapper_attributes' => [
              'class' => [
                'block',
              ],
            ],
          ];
          $form['dashboard_blocks_table'][$key]['column'] = [
            '#type' => 'select',
            '#default_value' => $relation['column'],
            '#required' => TRUE,
            '#title' => $this
              ->t('Region for @block block', [
              '@block' => $block_instance
                ->label(),
            ]),
            '#title_display' => 'invisible',
            '#options' => $regionOptions,
            '#attributes' => [
              'class' => [
                'block-region-select',
                'block-region-' . $region,
              ],
            ],
          ];
          $form['dashboard_blocks_table'][$key]['weight'] = [
            '#type' => 'weight',
            '#default_value' => $relation['weight'],
            '#delta' => $weight_delta,
            '#title' => t('Weight for @block block', [
              '@block' => $block_instance
                ->label(),
            ]),
            '#title_display' => 'invisible',
            '#attributes' => [
              'class' => [
                'block-weight',
                'block-weight-' . $region,
              ],
            ],
          ];
          $links = [];
          $links['edit'] = [
            'title' => $this
              ->t('Configure'),
            'url' => Url::fromRoute('draggable_dashboard.block_configure', [
              'block_id' => $key,
              'dashboard_entity' => $dashboard
                ->id(),
            ]),
          ];
          $links['delete'] = [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('draggable_dashboard.block_delete', [
              'dashboard_entity' => $dashboard
                ->id(),
              'block_id' => $key,
            ]),
          ];
          $form['dashboard_blocks_table'][$key]['operations'] = [
            'data' => [
              '#type' => 'operations',
              '#links' => $links,
            ],
            '#wrapper_attributes' => [
              'class' => [
                'text-align-right',
              ],
            ],
          ];
        }
      }
    }
    $form['actions']['back'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Back To Dashboards'),
      '#url' => new Url('entity.dashboard_entity.collection'),
      '#attributes' => [
        'class' => [
          'button',
        ],
      ],
    ];
  }
  return $form;
}