You are here

public function ContentDevelGenerate::settingsForm in Devel 4.x

Same name and namespace in other branches
  1. 8.3 devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::settingsForm()
  2. 8 devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::settingsForm()
  3. 8.2 devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::settingsForm()

Returns the form for the plugin.

Return value

array The array of default setting values, keyed by setting names.

Overrides DevelGenerateBase::settingsForm

File

devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php, line 213

Class

ContentDevelGenerate
Provides a ContentDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $types = $this->nodeTypeStorage
    ->loadMultiple();
  if (empty($types)) {
    $create_url = $this->urlGenerator
      ->generateFromRoute('node.type_add');
    $this
      ->setMessage($this
      ->t('You do not have any content types that can be generated. <a href=":create-type">Go create a new content type</a>', [
      ':create-type' => $create_url,
    ]), 'error', FALSE);
    return;
  }
  $options = [];
  foreach ($types as $type) {
    $options[$type
      ->id()] = [
      'type' => [
        '#markup' => $type
          ->label(),
      ],
    ];
    if ($this->commentManager) {
      $comment_fields = $this->commentManager
        ->getFields('node');
      $map = [
        $this
          ->t('Hidden'),
        $this
          ->t('Closed'),
        $this
          ->t('Open'),
      ];
      $fields = [];
      foreach ($comment_fields as $field_name => $info) {

        // Find all comment fields for the bundle.
        if (in_array($type
          ->id(), $info['bundles'])) {
          $instance = FieldConfig::loadByName('node', $type
            ->id(), $field_name);
          $default_value = $instance
            ->getDefaultValueLiteral();
          $default_mode = reset($default_value);
          $fields[] = new FormattableMarkup('@field: @state', [
            '@field' => $instance
              ->label(),
            '@state' => $map[$default_mode['status']],
          ]);
        }
      }

      // @todo Refactor display of comment fields.
      if (!empty($fields)) {
        $options[$type
          ->id()]['comments'] = [
          'data' => [
            '#theme' => 'item_list',
            '#items' => $fields,
          ],
        ];
      }
      else {
        $options[$type
          ->id()]['comments'] = $this
          ->t('No comment fields');
      }
    }
  }
  $header = [
    'type' => $this
      ->t('Content type'),
  ];
  if ($this->commentManager) {
    $header['comments'] = [
      'data' => $this
        ->t('Comments'),
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ];
  }
  $form['node_types'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
  ];
  $form['kill'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('<strong>Delete all content</strong> in these content types before generating new content.'),
    '#default_value' => $this
      ->getSetting('kill'),
  ];
  $form['num'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('How many nodes would you like to generate?'),
    '#default_value' => $this
      ->getSetting('num'),
    '#required' => TRUE,
    '#min' => 0,
  ];
  $options = [
    1 => $this
      ->t('Now'),
  ];
  foreach ([
    3600,
    86400,
    604800,
    2592000,
    31536000,
  ] as $interval) {
    $options[$interval] = $this->dateFormatter
      ->formatInterval($interval, 1) . ' ' . $this
      ->t('ago');
  }
  $form['time_range'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('How far back in time should the nodes be dated?'),
    '#description' => $this
      ->t('Node creation dates will be distributed randomly from the current time, back to the selected time.'),
    '#options' => $options,
    '#default_value' => 604800,
  ];
  $form['max_comments'] = [
    '#type' => $this->moduleHandler
      ->moduleExists('comment') ? 'number' : 'value',
    '#title' => $this
      ->t('Maximum number of comments per node.'),
    '#description' => $this
      ->t('You must also enable comments for the content types you are generating. Note that some nodes will randomly receive zero comments. Some will receive the max.'),
    '#default_value' => $this
      ->getSetting('max_comments'),
    '#min' => 0,
    '#access' => $this->moduleHandler
      ->moduleExists('comment'),
  ];
  $form['title_length'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum number of words in titles'),
    '#default_value' => $this
      ->getSetting('title_length'),
    '#required' => TRUE,
    '#min' => 1,
    '#max' => 255,
  ];
  $form['add_type_label'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Prefix the title with the content type label.'),
    '#description' => $this
      ->t('This will not count against the maximum number of title words specified above.'),
    '#default_value' => $this
      ->getSetting('add_type_label'),
  ];
  $form['add_alias'] = [
    '#type' => 'checkbox',
    '#disabled' => !$this->moduleHandler
      ->moduleExists('path'),
    '#description' => $this
      ->t('Requires path.module'),
    '#title' => $this
      ->t('Add an url alias for each node.'),
    '#default_value' => FALSE,
  ];
  $form['add_statistics'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Add statistics for each node (node_counter table).'),
    '#default_value' => TRUE,
    '#access' => $this->moduleHandler
      ->moduleExists('statistics'),
  ];

  // Add the language and translation options.
  $form += $this
    ->getLanguageForm('nodes');

  // Add the user selection checkboxes.
  $author_header = [
    'id' => $this
      ->t('User ID'),
    'user' => $this
      ->t('Name'),
    'role' => $this
      ->t('Role(s)'),
  ];
  $author_rows = [];

  /** @var \Drupal\user\UserInterface $user */
  foreach ($this->userStorage
    ->loadMultiple() as $user) {
    $author_rows[$user
      ->id()] = [
      'id' => [
        '#markup' => $user
          ->id(),
      ],
      'user' => [
        '#markup' => $user
          ->getAccountName(),
      ],
      'role' => [
        '#markup' => implode(", ", $user
          ->getRoles()),
      ],
    ];
  }
  $form['authors-wrap'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Users'),
    '#open' => FALSE,
    '#description' => $this
      ->t('Select users for randomly assigning as authors of the generated content. Leave all unchecked to use a random selection of up to 50 users.'),
  ];
  $form['authors-wrap']['authors'] = [
    '#type' => 'tableselect',
    '#header' => $author_header,
    '#options' => $author_rows,
  ];
  $form['#redirect'] = FALSE;
  return $form;
}