You are here

public function GitDirtyTreeSensorPlugin::buildConfigurationForm in Monitoring 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides SensorPluginBase::buildConfigurationForm

File

src/Plugin/monitoring/SensorPlugin/GitDirtyTreeSensorPlugin.php, line 290
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\GitDirtyTreeSensorPlugin.

Class

GitDirtyTreeSensorPlugin
Monitors the git repository for dirty files.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $form['repo_path'] = array(
    '#type' => 'textfield',
    '#default_value' => $this->sensorConfig
      ->getSetting('repo_path'),
    '#title' => t('Repository path'),
    '#description' => t('Path to the Git repository relative to the Drupal root directory.'),
  );
  $branches = $this
    ->runCommand('branches_cmd', t('Failing to get Git branches, Git might not be available.'));
  $expected_branch = $this->sensorConfig
    ->getSetting('expected_branch');
  if (empty($expected_branch)) {
    $expected_branch = $this
      ->runCommand('actual_branch_cmd', t('Failing to get the actual branch, Git might not be available.'));
  }
  $options = array();
  foreach ($branches as $branch) {
    $options[$branch] = $branch;
  }
  $form['check_branch'] = array(
    '#type' => 'checkbox',
    '#default_value' => $this->sensorConfig
      ->getSetting('check_branch'),
    '#title' => t('Branch control'),
    '#description' => t('Check if the current branch is different from the selected.'),
    '#disabled' => !$options,
  );
  $form['expected_branch'] = array(
    '#type' => 'select',
    '#default_value' => $expected_branch,
    '#maxlength' => 255,
    '#empty_option' => t('- Select -'),
    '#options' => $options,
    '#title' => t('Expected active branch'),
    '#description' => t('The branch that is going to be checked out.'),
    '#states' => array(
      // Hide the branch selector when the check_branch checkbox is disabled.
      'invisible' => array(
        ':input[name="settings[check_branch]"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );
  return $form;
}