You are here

public function AssetInjectorFormBase::form in Asset Injector 8

Same name and namespace in other branches
  1. 8.2 src/Form/AssetInjectorFormBase.php \Drupal\asset_injector\Form\AssetInjectorFormBase::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

2 calls to AssetInjectorFormBase::form()
CssInjectorForm::form in src/Form/CssInjectorForm.php
Gets the actual form array to be built.
JsInjectorForm::form in src/Form/JsInjectorForm.php
Gets the actual form array to be built.
2 methods override AssetInjectorFormBase::form()
CssInjectorForm::form in src/Form/CssInjectorForm.php
Gets the actual form array to be built.
JsInjectorForm::form in src/Form/JsInjectorForm.php
Gets the actual form array to be built.

File

src/Form/AssetInjectorFormBase.php, line 20

Class

AssetInjectorFormBase
Class CssInjectorForm.

Namespace

Drupal\asset_injector\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\asset_injector\Entity\AssetInjectorBase $entity */
  $entity = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $entity
      ->label(),
    '#description' => $this
      ->t('Label for the @type.', [
      '@type' => $entity
        ->getEntityType()
        ->getLabel(),
    ]),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $entity
      ->id(),
    '#machine_name' => [
      'exists' => '\\' . $entity
        ->getEntityType()
        ->getClass() . '::load',
      'replace_pattern' => '[^a-z0-9_]+',
      'replace' => '_',
    ],
    '#disabled' => !$entity
      ->isNew(),
  ];
  $form['code'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Code'),
    '#description' => $this
      ->t('The actual code goes in here.'),
    '#rows' => 10,
    '#default_value' => $entity->code,
    '#required' => TRUE,
    '#prefix' => '<div>',
    '#suffix' => '<div class="resizable"><div class="ace-editor"></div></div></div>',
  ];

  // Advanced options fieldset.
  $form['advanced'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Advanced options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $node_types = NodeType::loadMultiple();
  foreach ($node_types as $key => &$type) {
    $type = $type
      ->get('name');
  }
  $node_types[''] = $this
    ->t('-- None --');
  asort($node_types);
  $form['advanced']['nodeType'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Node Type'),
    '#options' => $node_types,
    '#default_value' => $entity->nodeType,
  ];
  $themes = [];
  foreach (system_list('theme') as $key => $theme) {
    if ($theme->status) {
      $themes[$key] = $theme->info['name'];
    }
  }
  $form['advanced']['themes'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Theme'),
    '#description' => $this
      ->t('Which themes is the @type used. Leave unselected to apply to all themes.', [
      '@type' => $entity
        ->getEntityType()
        ->getLabel(),
    ]),
    '#options' => $themes,
    '#multiple' => TRUE,
    '#default_value' => $entity->themes,
  ];
  $form['page_visibility'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Pages'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="nodeType"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $form['page_visibility']['visibility'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Add @type to specific pages', [
      '@type' => $entity
        ->getEntityType()
        ->getLabel(),
    ]),
    '#options' => [
      0 => $this
        ->t('Every page except the listed pages'),
      1 => $this
        ->t('The listed pages only'),
    ],
    '#default_value' => $entity->visibility ? $entity->visibility : 0,
  ];
  $form['page_visibility']['pages'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Pages'),
    '#title_display' => 'invisible',
    '#default_value' => $entity->pages,
    '#description' => $this
      ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
      '%blog' => '/blog',
      '%blog-wildcard' => '/blog/*',
      '%front' => '<front>',
    ]),
    '#rows' => 5,
  ];
  $form['#attached']['library'][] = 'asset_injector/ace-editor';
  return $form;
}