You are here

public function FlagFormBase::buildForm in Flag 8.4

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

2 calls to FlagFormBase::buildForm()
FlagAddForm::buildForm in src/Form/FlagAddForm.php
Form constructor.
FlagEditForm::buildForm in src/Form/FlagEditForm.php
Form constructor.
2 methods override FlagFormBase::buildForm()
FlagAddForm::buildForm in src/Form/FlagAddForm.php
Form constructor.
FlagEditForm::buildForm in src/Form/FlagEditForm.php
Form constructor.

File

src/Form/FlagFormBase.php, line 72

Class

FlagFormBase
Provides the base flag add/edit form.

Namespace

Drupal\flag\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type = NULL) {
  $form = parent::buildForm($form, $form_state);
  $flag = $this->entity;
  $form['#flag'] = $flag;
  $form['#flag_name'] = $flag
    ->id();
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $flag
      ->label(),
    '#description' => $this
      ->t('A short, descriptive title for this flag. It will be used in administrative interfaces to refer to this flag, and in page titles and menu items of some views this module provides (these are customizable, though). Some examples could be <em>Bookmarks</em>, <em>Favorites</em>, or <em>Offensive</em>.'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -3,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Machine name'),
    '#default_value' => $flag
      ->id(),
    '#description' => $this
      ->t('The machine-name for this flag. It may be up to 32 characters long and may only contain lowercase letters, underscores, and numbers. It will be used in URLs and in all API calls.'),
    '#weight' => -2,
    '#machine_name' => [
      'exists' => '\\Drupal\\flag\\Entity\\Flag::load',
    ],
    '#disabled' => !$flag
      ->isNew(),
    '#required' => TRUE,
  ];
  $form['global'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Scope'),
    '#default_value' => $flag
      ->isGlobal() ? 1 : 0,
    '#options' => [
      0 => $this
        ->t('Personal'),
      1 => $this
        ->t('Global'),
    ],
    '#weight' => -1,
  ];

  // Add descriptions for each radio button.
  $form['global'][0]['#description'] = $this
    ->t('Each user has individual flags on entities.');
  $form['global'][1]['#description'] = $this
    ->t('The entity is either flagged or not for all users.');
  $form['messages'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Messages'),
  ];
  $form['messages']['flag_short'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Flag link text'),
    '#default_value' => $flag
      ->get('flag_short') ?: $this
      ->t('Flag this item'),
    '#description' => $this
      ->t('The text for the "flag this" link for this flag.'),
    '#required' => TRUE,
  ];
  $form['messages']['flag_long'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Flag link description'),
    '#default_value' => $flag
      ->get('flag_long'),
    '#description' => $this
      ->t('The description of the "flag this" link. Usually displayed on mouseover.'),
  ];
  $form['messages']['flag_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Flagged message'),
    '#default_value' => $flag
      ->get('flag_message'),
    '#description' => $this
      ->t('Message displayed after flagging content. If JavaScript is enabled, it will be displayed below the link. If not, it will be displayed in the message area.'),
  ];
  $form['messages']['unflag_short'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Unflag link text'),
    '#default_value' => $flag
      ->get('unflag_short') ?: $this
      ->t('Unflag this item'),
    '#description' => $this
      ->t('The text for the "unflag this" link for this flag.'),
    '#required' => TRUE,
  ];
  $form['messages']['unflag_long'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Unflag link description'),
    '#default_value' => $flag
      ->get('unflag_long'),
    '#description' => $this
      ->t('The description of the "unflag this" link. Usually displayed on mouseover.'),
  ];
  $form['messages']['unflag_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Unflagged message'),
    '#default_value' => $flag
      ->get('unflag_message'),
    '#description' => $this
      ->t('Message displayed after content has been unflagged. If JavaScript is enabled, it will be displayed below the link. If not, it will be displayed in the message area.'),
  ];
  $form['access'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Flag access'),
    '#tree' => FALSE,
    '#weight' => 10,
  ];

  // Switch plugin type in case a different is chosen.
  $flag_type_plugin = $flag
    ->getFlagTypePlugin();
  $flag_type_def = $flag_type_plugin
    ->getPluginDefinition();
  $bundles = $this->bundleInfoService
    ->getBundleInfo($flag_type_def['entity_type']);
  $entity_bundles = [];
  foreach ($bundles as $bundle_id => $bundle_row) {
    $entity_bundles[$bundle_id] = $bundle_row['label'];
  }

  // Flag classes will want to override this form element.
  $form['access']['bundles'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Flaggable types'),
    '#options' => $entity_bundles,
    '#default_value' => $flag
      ->getBundles(),
    '#description' => $this
      ->t('Check any bundles that this flag may be used on. Leave empty to apply to all bundles.'),
    '#weight' => 10,
  ];
  $form['access']['unflag_denied_text'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Unflag not allowed text'),
    '#default_value' => $flag
      ->getUnflagDeniedText(),
    '#description' => $this
      ->t('If a user is allowed to flag but not unflag, this text will be displayed after flagging. Often this is the past-tense of the link text, such as "flagged".'),
    '#weight' => -1,
  ];
  $form['display'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Display options'),
    '#description' => $this
      ->t('Flags are usually controlled through links that allow users to toggle their behavior. You can choose how users interact with flags by changing options here. It is legitimate to have none of the following checkboxes ticked, if, for some reason, you wish <a href="@placement-url">to place the the links on the page yourself</a>.', [
      '@placement-url' => 'http://drupal.org/node/295383',
    ]),
    '#tree' => FALSE,
    '#weight' => 20,
    '#prefix' => '<div id="link-type-settings-wrapper">',
    '#suffix' => '</div>',
  ];
  $form['display']['settings'] = [
    '#type' => 'container',
    '#weight' => 21,
  ];
  $form = $flag_type_plugin
    ->buildConfigurationForm($form, $form_state);
  $form['display']['link_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Link type'),
    '#options' => $this->actionLinkManager
      ->getAllLinkTypes(),
    // '#after_build' => array('flag_check_link_types'),
    '#default_value' => $flag
      ->getLinkTypePlugin()
      ->getPluginId(),
    // Give this a high weight so additions by the flag classes for entity-
    // specific options go above.
    '#weight' => 18,
    '#attributes' => [
      'class' => [
        'flag-link-options',
      ],
    ],
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::updateSelectedPluginType',
      'wrapper' => 'link-type-settings-wrapper',
      'event' => 'change',
      'method' => 'replace',
    ],
  ];

  //debug($flag->getLinkTypePlugin()->getPluginId(), 'default value');
  $form['display']['link_type_submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Update'),
    '#submit' => [
      '::submitSelectPlugin',
    ],
    '#weight' => 20,
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
  ];

  // Add the descriptions to each ratio button element. These attach to the
  // elements when FormAPI expands them.
  $action_link_plugin_defs = $this->actionLinkManager
    ->getDefinitions();
  foreach ($action_link_plugin_defs as $key => $info) {
    $form['display']['link_type'][$key] = [
      '#description' => $info['description'],
      '#executes_submit_callback' => TRUE,
      '#limit_validation_errors' => [
        [
          'link_type',
        ],
      ],
      '#submit' => [
        '::submitSelectPlugin',
      ],
    ];
  }
  $action_link_plugin = $flag
    ->getLinkTypePlugin();
  $form = $action_link_plugin
    ->buildConfigurationForm($form, $form_state);
  return $form;
}