You are here

public function Expert::alterForm in Display Suite 8.4

Same name and namespace in other branches
  1. 8.2 src/Plugin/DsFieldTemplate/Expert.php \Drupal\ds\Plugin\DsFieldTemplate\Expert::alterForm()
  2. 8.3 src/Plugin/DsFieldTemplate/Expert.php \Drupal\ds\Plugin\DsFieldTemplate\Expert::alterForm()

Lets you add you add additional form element for your layout.

Parameters

array $form: Nested array of form elements that comprise the form.

Overrides DsFieldTemplateBase::alterForm

File

src/Plugin/DsFieldTemplate/Expert.php, line 19

Class

Expert
Plugin for the expert field template.

Namespace

Drupal\ds\Plugin\DsFieldTemplate

Code

public function alterForm(&$form) {
  $config = $this
    ->getConfiguration();

  // Add label.
  $form['lb'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#size' => '10',
    '#default_value' => $config['lb'],
  ];

  // Add prefix.
  $form['prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Prefix'),
    '#size' => '100',
    '#description' => $this
      ->t('You can enter any html in here.'),
    '#default_value' => isset($config['prefix']) ? $config['prefix'] : '',
    '#prefix' => '<div class="field-prefix">',
    '#suffix' => '</div>',
  ];
  $wrappers = [
    'lbw' => [
      'title' => $this
        ->t('Label wrapper'),
    ],
    'ow' => [
      'title' => $this
        ->t('Outer wrapper'),
    ],
    'fis' => [
      'title' => $this
        ->t('Field items'),
    ],
    'fi' => [
      'title' => $this
        ->t('Field item'),
    ],
  ];
  foreach ($wrappers as $wrapper_key => $value) {
    $form[$wrapper_key] = [
      '#type' => 'checkbox',
      '#title' => $value['title'],
      '#prefix' => '<div class="ft-group ' . $wrapper_key . '">',
      '#default_value' => $config[$wrapper_key],
    ];
    $form[$wrapper_key . '-el'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Element'),
      '#size' => '10',
      '#description' => $this
        ->t('E.g. div, span, h2 etc.'),
      '#default_value' => $config[$wrapper_key . '-el'],
      '#states' => [
        'visible' => [
          ':input[name$="[' . $wrapper_key . ']"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form[$wrapper_key . '-cl'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Classes'),
      '#size' => '10',
      '#default_value' => $config[$wrapper_key . '-cl'],
      '#description' => $this
        ->t('E.g. field-expert'),
      '#states' => [
        'visible' => [
          ':input[name$="[' . $wrapper_key . ']"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form[$wrapper_key . '-at'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Attributes'),
      '#size' => '20',
      '#default_value' => $config[$wrapper_key . '-at'],
      '#description' => $this
        ->t('E.g. name="anchor"'),
      '#states' => [
        'visible' => [
          ':input[name$="[' . $wrapper_key . ']"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Hide colon.
    if ($wrapper_key == 'lbw') {
      $form['lb-col'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Show label colon'),
        '#default_value' => $config['lb-col'],
        '#attributes' => [
          'class' => [
            'colon-checkbox',
          ],
        ],
        '#states' => [
          'visible' => [
            ':input[name$="[' . $wrapper_key . ']"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    if ($wrapper_key != 'lbw') {
      $form[$wrapper_key . '-def-at'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Add default attributes'),
        '#default_value' => $config[$wrapper_key . '-def-at'],
        '#suffix' => $wrapper_key == 'ow' ? '' : '</div><div class="clearfix"></div>',
        '#states' => [
          'visible' => [
            ':input[name$="[' . $wrapper_key . ']"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    else {
      $form['ft'][$wrapper_key . '-def-at'] = [
        '#markup' => '</div><div class="clearfix"></div>',
      ];
    }

    // Default classes for outer wrapper.
    if ($wrapper_key == 'ow') {
      $form[$wrapper_key . '-def-cl'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Add default classes'),
        '#default_value' => $config[$wrapper_key . '-def-cl'],
        '#suffix' => '</div><div class="clearfix"></div>',
        '#states' => [
          'visible' => [
            ':input[name$="[' . $wrapper_key . ']"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
  }

  // Add suffix.
  $form['suffix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Suffix'),
    '#size' => '100',
    '#description' => $this
      ->t('You can enter any html in here.'),
    '#default_value' => isset($config['suffix']) ? $config['suffix'] : '',
    '#prefix' => '<div class="field-suffix">',
    '#suffix' => '</div>',
  ];

  // Token support.
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $form['tokens'] = [
      '#title' => $this
        ->t('Tokens'),
      '#type' => 'container',
      '#states' => [
        'invisible' => [
          'input[name="use_token"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['tokens']['help'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => 'all',
      '#global_types' => FALSE,
      '#dialog' => TRUE,
    ];
  }
}