0

This question already has an answer here:

Need help on how to create a custom form that is only available for admins that will query all content types and display the options as a checkbox.

The goal is to only add certain libraries to particular content types selectively through an admin form. How would i query all content types and display them as checkboxes?

What we tried:

mymodule.module

function mymodue_preprocess_node(&$variables) {
  $node = $variables['node'];
  $config = \Drupal::config('mymodule.settings');
  $enabled_content_types = !empty($config->get('enabled_content_types')) ? $config->get('enabled_content_types') : FALSE;

  // This is where we need help.
  if(in_array($enabled_content_type, $node->bundle()) {
    // perform some logic.
  }  
}

mymoduleForm.php

<?php

namespace Drupal\mymodule\Form;

class mymoduleForm extends ConfigFormBase {
  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'mymodule.settings',
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $types = \Drupal::entityTypeManager()
      ->getStorage('node_type')
      ->loadMultiple();

    foreach($types as $type) {
      $content_types[$type->id()] = $type->label();
    }

    // Need help setting this up so that the options are content types. 
    $form['enabled_content_types'] = [
      '#type' => 'checkbox',
      '#title' => 'Enabled Content Types',
      '#description' => 'Run preprocess for all enabled content types only',
      '#options' => $content_types,
    ],
  }
}

marked as duplicate by leymannx, DRUPWAY, mradcliffe, kiamlaluno Jul 21 '18 at 21:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @leymannx although that got me closer by showing me how to query all the content types, i am still not able to display those values as a checkbox as it only allows for a single value to be selected. i have updated the code to reflect this. – usernameabc Jul 17 '18 at 19:46
  • Use '#type' => 'radios'. Sample code: api.drupal.org/api/drupal/… – leymannx Jul 17 '18 at 20:44
  • 1
    Or '#type' => 'checkboxes'. Plural. Sample code: api.drupal.org/api/drupal/… – leymannx Jul 17 '18 at 20:47
  • @leymannx that did it, the sample code worked like a charm. I will add a comment with the modification i made. – usernameabc Jul 17 '18 at 22:44
1

This was inspired from @leymannx comments.

I changed the '#type' to checkboxes.

/**
 * {@inheritdoc}
 */
public function buildForm(array $form, FormStateInterface $form_state) {
 $types = \Drupal::entityTypeManager()
   ->getStorage('node_type')
   ->loadMultiple();

 foreach($types as $type) {
   $content_types[$type->id()] = $type->label();
 }

 // Need help setting this up so that the options are content types. 
 $form['enabled_content_types'] = [
   '#type' => 'checkboxes',
   '#title' => 'Enabled Content Types',
   '#description' => 'Run preprocess for all enabled content types only',
   '#options' => $content_types,
 ],
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.