You are here

public function FontDisplayForm::form in @font-your-face 8.3

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/Form/FontDisplayForm.php, line 20

Class

FontDisplayForm
Form to display fonts.

Namespace

Drupal\fontyourface\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $font_display = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $font_display
      ->label(),
    '#description' => $this
      ->t("Label for the Font display."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $font_display
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\fontyourface\\Entity\\FontDisplay::load',
    ],
    '#disabled' => !$font_display
      ->isNew(),
  ];

  /* You will need additional form elements for your custom properties. */
  $fonts = Font::loadActivatedFonts();
  if (empty($fonts)) {
    \Drupal::messenger()
      ->addMessage($this
      ->t('Please enable at least one font before creating/updating a font style.'), 'warning');
    $this
      ->redirect('entity.font.collection')
      ->send();
    exit;
  }
  $available_fonts = [];
  foreach ($fonts as $font) {
    $available_fonts[$font->url->value] = $font->name->value;
  }
  $drupal_themes = \Drupal::service('theme_handler')
    ->listInfo();
  $themes = [];
  foreach ($drupal_themes as $key => $theme) {
    if (!empty($theme->info['hidden'])) {
      continue;
    }
    $themes[$key] = $theme->info['name'];
  }
  $form['font_url'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Font'),
    '#description' => $this
      ->t('Select the font to use as part of the font style'),
    '#default_value' => $font_display
      ->getFontUrl(),
    '#options' => $available_fonts,
    '#required' => TRUE,
  ];
  foreach ($fonts as $font) {
    $element_id = 'font_display_usage_' . $font
      ->Id();
    $form[$element_id] = [
      '#type' => 'container',
      '#states' => [
        'visible' => [
          'select[name="font_url"]' => [
            'value' => $font->url->value,
          ],
        ],
      ],
    ];
    $form[$element_id]['usage'] = [
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#title' => 'Usage',
    ];
    $form[$element_id]['usage']['instructions'] = [
      '#type' => 'item',
      '#markup' => 'If you wish to skip using the font display and add the css directly to your theme, copy/paste the following for the font into your theme css file:',
    ];
    $form[$element_id]['usage']['preview'] = [
      '#type' => 'html_tag',
      '#tag' => 'code',
      '#attributes' => [
        'style' => 'white-space: pre;',
      ],
      '#value' => fontyourface_font_css($font, NULL, "\n"),
    ];
  }
  $form['fallback'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Fallback fonts'),
    '#description' => $this
      ->t('Fallback fonts in case selected font fails to load.'),
    '#default_value' => $font_display
      ->getFallback(),
  ];
  $preset_selectors = $this
    ->getPresetSelectors();
  $form['preset_selectors'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Preset Selectors'),
    '#description' => $this
      ->t('Use preset selectors to easily display your font.'),
    '#options' => $preset_selectors,
    '#default_value' => $this
      ->getDefaultSelectorOption($font_display),
    '#required' => TRUE,
  ];
  $form['selectors'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Selectors'),
    '#description' => $this
      ->t('Selects the selected font will apply to. Note that all pages will have a "fontyourface" class on the body tag. You can use that to specify a font.'),
    '#default_value' => $font_display
      ->getSelectors(),
    '#maxlength' => 300,
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        'select[name="preset_selectors"]' => [
          'value' => 'other',
        ],
      ],
    ],
  ];
  $form['theme'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Theme'),
    '#description' => $this
      ->t('Select theme this display will work for.'),
    '#default_value' => $font_display
      ->getTheme(),
    '#options' => $themes,
    '#required' => TRUE,
  ];
  return $form;
}