Same name in other branches
  1. 8.x-3.x src/Form/QuickTabsInstanceEditForm.php \Drupal\quicktabs\Form\QuickTabsInstanceEditForm::form()

File

src/Form/QuickTabsInstanceEditForm.php, line 78

Class

QuickTabsInstanceEditForm
Class QuickTabsInstanceEditForm.

Namespace

Drupal\quicktabs\Form

Code

public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['label'] = [
        '#title' => $this->t('Name'),
        '#description' => $this->t('This will appear as the block title.'),
        '#type' => 'textfield',
        '#default_value' => $this->entity
            ->label(),
        '#weight' => -9,
        '#required' => TRUE,
        '#placeholder' => $this->t('Enter name'),
    ];
    $form['id'] = [
        '#type' => 'machine_name',
        '#maxlength' => 32,
        '#required' => TRUE,
        '#default_value' => $this->entity
            ->id(),
        '#machine_name' => [
            'exists' => 'Drupal\\quicktabs\\Entity\\QuickTabsInstance::getQuickTabsInstance',
        ],
        '#description' => $this->t('A unique machine-readable name for this QuickTabs instance. It must only contain lowercase letters, numbers, and underscores. The machine name will be used internally by QuickTabs and will be used in the CSS ID of your QuickTabs block.'),
        '#weight' => -8,
    ];
    // Instantiate all TabRenderer plugins.
    $plugin_definitions = $this->tabRendererManager
        ->getDefinitions();
    $renderers = [];
    $renderer_form_options = [];
    // Use the name of each plugin to create the dropdown.
    foreach ($plugin_definitions as $index => $def) {
        $renderers[$index] = $def['name']->__toString();
        $object = $this->tabRendererManager
            ->createInstance($index);
        $renderer_form_options[$index] = $object->optionsForm($this->entity);
    }
    $form['renderer'] = [
        '#type' => 'select',
        '#title' => $this->t('Renderer'),
        '#options' => $renderers,
        '#default_value' => $this->entity
            ->getRenderer(),
        '#description' => $this->t('Choose how to render the content.'),
        '#weight' => -7,
    ];
    // Add the renderer options form elements to the form.
    // Use Drupal Form states API to show only selected renderer options.
    $form['options'] = [
        '#tree' => TRUE,
        '#weight' => -6,
    ];
    foreach ($renderer_form_options as $renderer => $options) {
        foreach ($options as &$option) {
            $option['#states'] = [
                'visible' => [
                    ':input[name="renderer"]' => [
                        'value' => $renderer,
                    ],
                ],
            ];
        }
        $form['options'][$renderer] = $options;
    }
    $form['hide_empty_tabs'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Hide empty tabs'),
        '#default_value' => $this->entity
            ->getHideEmptyTabs(),
        '#description' => $this->t('Empty and restricted tabs will not be displayed. Could be useful when the tab content is not accessible.<br />This option does not work in ajax mode.'),
        '#weight' => -3,
    ];
    $tab_titles = [
        QuickTabsInstance::QUICKTABS_DELTA_NONE => $this->t('- None -'),
    ];
    // Create a table with each tr corresponding to a tab.
    $qt = new \stdClass();
    if (!empty($form_state->getValue('configuration_data'))) {
        $qt->tabs = $form_state->getValue('configuration_data');
    }
    else {
        $qt->tabs = $this->entity
            ->getConfigurationData();
    }
    // Show 2 empty tabs when adding a new QT instance.
    if (empty($qt->tabs)) {
        $qt->tabs = [
            0 => [],
            1 => [],
        ];
    }
    else {
        if (is_numeric($form_state->get('to_remove'))) {
            unset($qt->tabs[$form_state->get('to_remove')]);
            $form_state->set('num_tabs', $form_state->get('num_tabs') - 1);
        }
        // If the number of rows has been incremented
        // add another row.
        if ($form_state->get('num_tabs') > count($qt->tabs)) {
            $qt->tabs[] = [];
        }
    }
    $delta = 0;
    if (!empty($qt->tabs)) {
        foreach ($qt->tabs as $tab) {
            if (!empty($tab)) {
                $tab_titles[$delta] = $tab['title'];
                $delta++;
            }
        }
    }
    $form['default_tab'] = [
        '#type' => 'select',
        '#title' => $this->t('Default tab'),
        '#options' => $tab_titles,
        '#default_value' => $this->entity
            ->getDefaultTab(),
        '#access' => !empty($tab_titles),
        '#weight' => -4,
    ];
    $form['configuration_data_wrapper'] = [
        '#tree' => FALSE,
        '#weight' => -3,
        '#prefix' => '<div class="clear-block" id="configuration-data-wrapper">',
        '#suffix' => '</div>',
    ];
    $form['configuration_data_wrapper']['configuration_data'] = $this->getConfigurationDataForm($qt);
    // There are two functions attached to the more button:
    // The submit function will be called first; increments number of rows.
    // The callback function will then return the rendered rows.
    $form['tabs_more'] = [
        '#name' => 'tabs_more',
        '#type' => 'submit',
        '#value' => $this->t('Add tab'),
        '#attributes' => [
            'class' => [
                'add-tab',
            ],
            'title' => $this->t('Click here to add more tabs.'),
        ],
        '#weight' => 1,
        '#submit' => [
            [
                $this,
                'ajaxFormSubmit',
            ],
        ],
        '#ajax' => [
            'callback' => [
                $this,
                'ajaxFormCallback',
            ],
            'progress' => [
                'type' => 'throbber',
                'message' => NULL,
            ],
            'effect' => 'fade',
        ],
    ];
    // The form js will ensure that only one set of tab options is visible.
    $form['#attached']['library'][] = 'quicktabs/quicktabs.form';
    return $form;
}