function MediaDevelGenerate::settingsForm

Same name in other branches
  1. 5.x devel_generate/src/Plugin/DevelGenerate/MediaDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\MediaDevelGenerate::settingsForm()

Overrides DevelGenerateBase::settingsForm

File

devel_generate/src/Plugin/DevelGenerate/MediaDevelGenerate.php, line 150

Class

MediaDevelGenerate
Provides a plugin that generates media entities.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
    $types = $this->mediaTypeStorage
        ->loadMultiple();
    if (empty($types)) {
        $create_url = $this->urlGenerator
            ->generateFromRoute('entity.media_type.add_form');
        $this->setMessage($this->t('You do not have any media types that can be generated. <a href=":url">Go create a new media type</a>', [
            ':url' => $create_url,
        ]), MessengerInterface::TYPE_ERROR);
        return [];
    }
    $options = [];
    foreach ($types as $type) {
        $options[$type->id()] = [
            'type' => [
                '#markup' => $type->label(),
            ],
        ];
    }
    $form['media_types'] = [
        '#type' => 'tableselect',
        '#header' => [
            'type' => $this->t('Media type'),
        ],
        '#options' => $options,
    ];
    $form['kill'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('<strong>Delete all media</strong> in these types before generating new media.'),
        '#default_value' => $this->getSetting('kill'),
    ];
    $form['num'] = [
        '#type' => 'number',
        '#title' => $this->t('How many media items 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 media be dated?'),
        '#description' => $this->t('Media creation dates will be distributed randomly from the current time, back to the selected time.'),
        '#options' => $options,
        '#default_value' => 604800,
    ];
    $form['name_length'] = [
        '#type' => 'number',
        '#title' => $this->t('Maximum number of words in names'),
        '#default_value' => $this->getSetting('name_length'),
        '#required' => TRUE,
        '#min' => 1,
        '#max' => 255,
    ];
    $options = [];
    // We always need a language.
    $languages = $this->languageManager
        ->getLanguages(LanguageInterface::STATE_ALL);
    foreach ($languages as $langcode => $language) {
        $options[$langcode] = $language->getName();
    }
    $form['add_language'] = [
        '#type' => 'select',
        '#title' => $this->t('Set language on media'),
        '#multiple' => TRUE,
        '#description' => $this->t('Requires locale.module'),
        '#options' => $options,
        '#default_value' => [
            $this->languageManager
                ->getDefaultLanguage()
                ->getId(),
        ],
    ];
    $form['#redirect'] = FALSE;
    return $form;
}