You are here

public function FlagViewsRelationship::buildOptionsForm in Flag 8.4

Provide a form to edit options for this plugin.

Overrides RelationshipPluginBase::buildOptionsForm

File

src/Plugin/views/relationship/FlagViewsRelationship.php, line 90

Class

FlagViewsRelationship
Provides a views relationship to select flag content by a flag.

Namespace

Drupal\flag\Plugin\views\relationship

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $entity_type = $this->definition['flaggable'];
  $form['admin_label']['admin_label']['#description'] = $this
    ->t('The name of the selected flag makes a good label.');
  $flags = $this->flagService
    ->getAllFlags($entity_type);
  $form['flag'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Flag'),
    '#default_value' => $this->options['flag'],
    '#required' => TRUE,
  ];
  foreach ($flags as $flag_id => $flag) {
    $form['flag']['#options'][$flag_id] = $flag
      ->label();
  }
  $form['user_scope'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('By'),
    '#options' => [
      'current' => $this
        ->t('Current user'),
      'any' => $this
        ->t('Any user'),
    ],
    '#default_value' => $this->options['user_scope'],
  ];
  $form['required']['#title'] = $this
    ->t('Include only flagged content');
  $form['required']['#description'] = $this
    ->t('If checked, only content that has this flag will be included. Leave unchecked to include all content; or, in combination with the <em>Flagged</em> filter, <a href="@unflagged-url">to limit the results to specifically unflagged content</a>.', [
    '@unflagged-url' => 'http://drupal.org/node/299335',
  ]);
  if (!$form['flag']['#options']) {
    $form = [
      'error' => [
        '#markup' => '<p class="error form-item">' . $this
          ->t('No %type flags exist. You must first <a href="@create-url">create a %type flag</a> before being able to use this relationship type.', [
          '%type' => $entity_type,
          '@create-url' => Url::fromRoute('entity.flag.collection')
            ->toString(),
        ]) . '</p>',
      ],
    ];
  }
}