You are here

public function GoogleAdwordsAdminSettings::buildForm in Google AdWords Conversion Tracking 8

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 ConfigFormBase::buildForm

File

src/Form/GoogleAdwordsAdminSettings.php, line 70
Contains \Drupal\google_adwords\Form\GoogleAdwordsAdminSettings.

Class

GoogleAdwordsAdminSettings
Class GoogleAdwordsAdminSettings.

Namespace

Drupal\google_adwords\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('google_adwords.adminsettings');
  $form['conversion'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default Conversion settings'),
    '#collapsible' => FALSE,
  );
  $form['conversion']['conversion_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Conversion ID'),
    '#default_value' => $config
      ->get('conversion_id'),
    '#size' => 15,
    '#maxlength' => 255,
    '#required' => FALSE,
    '#description' => '',
  );
  $form['conversion']['language'] = array(
    '#type' => 'textfield',
    '#title' => t('Conversion Language'),
    '#default_value' => $config
      ->get('language'),
    '#size' => 15,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#description' => '',
  );
  $form['conversion']['format'] = array(
    '#type' => 'textfield',
    '#title' => t('Conversion Format'),
    '#default_value' => $config
      ->get('format'),
    '#size' => 15,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#description' => '',
  );
  $form['conversion']['color'] = array(
    '#type' => 'textfield',
    '#title' => t('Conversion Color'),
    '#default_value' => $config
      ->get('color'),
    '#size' => 15,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#description' => '',
  );
  $form['conversion']['external_script'] = array(
    '#type' => 'textfield',
    '#title' => t('External JavaScript'),
    '#default_value' => $config
      ->get('external_script'),
    '#size' => 80,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#description' => '',
  );

  // Render the role overview.
  $form['conversion']['roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('User Role Tracking'),
    '#collapsible' => TRUE,
    '#description' => t('Define what user roles should be tracked.'),
  );

  /**
   * @var \Drupal\user\RoleStorage $role_storage
   *   Storage object for role entities
   */
  $role_storage = $this->entity_type_manager
    ->getStorage('user_role');

  /**
   * @var int|array[/Drupal/Core/Entity] $roles
   */
  $roles = $role_storage
    ->getQuery()
    ->execute();
  if (is_array($roles)) {
    foreach ($role_storage
      ->loadMultiple($roles) as $role) {

      /**
       * @var \Drupal\user\Entity\Role $role
       *   each role entity
       */

      /**
       * @var string $var_name
       *   admin settings config var name for the role
       *
       * @note that role ids are no longer numeric ids in a table
       *   so their ids should be safe to use in keys
       */
      $var_name = 'google_adwords_track_' . $role
        ->id();
      $form['conversion']['roles'][$var_name] = array(
        '#type' => 'checkbox',
        '#title' => $role
          ->label(),
        // this should already be translated
        '#default_value' => $config
          ->get($var_name),
      );
    }
  }
  else {
    $form['conversion']['roles']['none'] = array(
      '#type' => 'markup',
      '#markup' => $this
        ->t('No roles found in the system. None can be marked for conversion'),
      '#prefix' => '<strong>',
      '#suffix' => '</strong>',
    );
  }
  return parent::buildForm($form, $form_state);
}