You are here

public function AddContentByBundle::buildOptionsForm in Add Content by Bundle Views Area Plugin 1.x

Provide a form to edit options for this plugin.

Overrides AreaPluginBase::buildOptionsForm

File

src/Plugin/views/area/AddContentByBundle.php, line 79

Class

AddContentByBundle
Defines an area plugin to display a bundle-specific node/add link.

Namespace

Drupal\add_content_by_bundle\Plugin\views\area

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);

  // Use the entity_type defined for the view.
  $entity_type = $this->view
    ->getBaseEntityType()
    ->id();

  // Get all bundle types for our entity type.
  $bundles = \Drupal::service('entity_type.bundle.info')
    ->getBundleInfo($entity_type);

  // Assembled an options list of the bundles.
  $bundlesList = [];
  foreach ($bundles as $id => $bundle) {
    $label = $bundle['label'];
    $bundlesList[$id] = $label;
  }

  // New content bundle type.
  // TODO: preselect if a single bundle specified for the view?
  $form['bundle'] = [
    '#title' => $this
      ->t('Add content bundle (Content) type'),
    '#description' => $this
      ->t('The bundle (content) type of content to add.'),
    '#type' => 'select',
    '#options' => $bundlesList,
    '#default_value' => !empty($this->options['bundle']) ? $this->options['bundle'] : '',
    '#required' => TRUE,
  ];

  // If the Form Mode Control module is installed, expose an option to use it.
  if (\Drupal::service('module_handler')
    ->moduleExists('form_mode_control')) {
    $form_modes = \Drupal::service('entity_display.repository')
      ->getFormModeOptions($entity_type);

    // Only expose the form element if our entity type has more than one
    // form mode.
    if ($form_modes && is_array($form_modes) && count($form_modes) > 1) {
      $form['form_mode'] = [
        '#title' => $this
          ->t('Form mode'),
        '#description' => $this
          ->t('The form mode to use for adding an entity.'),
        '#type' => 'select',
        '#options' => $form_modes,
        '#default_value' => !empty($this->options['form_mode']) ? $this->options['form_mode'] : '',
      ];
    }
  }
  $form['label'] = [
    '#title' => $this
      ->t('Label'),
    '#description' => $this
      ->t('The text of the link.'),
    '#type' => 'textfield',
    '#default_value' => $this->options['label'] ?: $this
      ->t('Add a new entry'),
    '#required' => TRUE,
  ];
  $form['class'] = [
    '#title' => $this
      ->t('Class'),
    '#description' => $this
      ->t('A CSS class to apply to the link. If using multiple classes, separate them by spaces.'),
    '#type' => 'textfield',
    '#default_value' => $this->options['class'],
  ];
  $form['target'] = [
    '#title' => $this
      ->t('Target'),
    '#description' => $this
      ->t('Optionally have the form open on-page in a modal or off-canvas dialog.'),
    '#type' => 'select',
    '#default_value' => $this->options['target'],
    '#options' => [
      '' => $this
        ->t('Default'),
      'tray' => $this
        ->t('Off-Screen Tray'),
      'modal' => $this
        ->t('Modal Dialog'),
    ],
  ];
  $form['width'] = [
    '#title' => $this
      ->t('Dialog Width'),
    '#description' => $this
      ->t('How wide the dialog should appear.'),
    '#type' => 'number',
    '#min' => '100',
    '#default_value' => $this->options['width'],
    '#states' => [
      // Show this number field only if a dialog is chosen above.
      'invisible' => [
        ':input[name="options[target]"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $form['params'] = [
    '#title' => $this
      ->t('Additional Parameters'),
    '#description' => $this
      ->t('List any additional paramters, separating the key and value with a pipe (|). The use of tokens for the view\'s arguments is supported. An example is {{ arguments.user_id }}.'),
    '#type' => 'textarea',
    '#default_value' => $this->options['params'],
  ];
}