You are here

class AdminSettings in Doubleclick for Publishers (DFP) 8

Defines a form that configures DFP global settings.

Hierarchy

Expanded class hierarchy of AdminSettings

1 string reference to 'AdminSettings'
dfp.routing.yml in ./dfp.routing.yml
dfp.routing.yml

File

src/Form/AdminSettings.php, line 21
Contains \Drupal\dfp\Form\AdminSettings.

Namespace

Drupal\dfp\Form
View source
class AdminSettings extends ConfigFormBase {
  use TargetingFormTrait;

  /**
   * Entity bundle information.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $bundleInfo;

  /**
   * Constructs a \Drupal\dfp\Form\AdminSettings object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeBundleInfoInterface $bundle_info) {
    parent::__construct($config_factory);
    $this->bundleInfo = $bundle_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity_type.bundle.info'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'dfp_admin_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'dfp.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
    $config = $this
      ->config('dfp.settings');

    // @todo vertical tabs?
    // Default tag settings.
    $form['global_tag_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Global Tag Settings'),
      '#open' => TRUE,
    ];
    $form['global_tag_settings']['network_id'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Network ID'),
      '#default_value' => $config
        ->get('network_id'),
      '#required' => TRUE,
      '#description' => $this
        ->t('The Network ID to use on all tags. According to Google this value should begin with a /.'),
    ];
    $form['global_tag_settings']['adunit_pattern'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default Ad Unit Pattern'),
      '#required' => FALSE,
      '#maxlength' => 255,
      '#default_value' => $config
        ->get('adunit_pattern'),
      '#description' => $this
        ->t('Use the tokens below to define how the default ad unit should display. This can be overridden on each tag. The network id will be included automatically. Example: [current-page:url:args:value:0]/[current-page:url:args:value:1]/[dfp_tag:slot]'),
    ];
    $form['global_tag_settings']['click_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('DFP Click URL (Sync mode only)'),
      '#default_value' => $config
        ->get('click_url'),
      '#description' => $this
        ->t('Sets a click URL on each DFP tag. Useful for intercepting ad click calls for reporting. Values beginning with http:// are treated as absolute; otherwise relative to current site domain.'),
      '#states' => [
        'enabled' => [
          'input[name="async_rendering"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];

    // @todo: Add back token browser.
    $form['global_tag_settings']['async_rendering'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Render ads asynchronously'),
      '#default_value' => $config
        ->get('async_rendering'),
      '#description' => $this
        ->t('This can speed up page rendering time by loading page content without waiting for ads to load.'),
    ];
    $form['global_tag_settings']['disable_init_load'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Disable initial ad load'),
      '#default_value' => $config
        ->get('disable_init_load'),
      '#description' => $this
        ->t('(Async mode only) Disables the initial fetch of ads from Google when the page is first loaded. Calls to refresh() can still be used to fetch ads.'),
      '#states' => [
        'enabled' => [
          'input[name="async_rendering"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['global_tag_settings']['single_request'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Combine all ad requests into a single request'),
      '#default_value' => $config
        ->get('single_request'),
      '#description' => $this
        ->t('This can speed up page rendering time by limiting the number of external requests.'),
    ];

    // Global display options.
    $form['global_display_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Global Display Options'),
      '#open' => TRUE,
    ];
    $form['global_display_options']['default_slug'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Global Slug'),
      '#default_value' => $config
        ->get('default_slug'),
      '#required' => FALSE,
      '#description' => $this
        ->t('Slug all ad tags with this label. Example: Advertisement'),
    ];
    $form['global_display_options']['collapse_empty_divs'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Collapse empty divs'),
      '#default_value' => $config
        ->get('collapse_empty_divs'),
      '#options' => [
        0 => $this
          ->t('Never'),
        1 => $this
          ->t('Collapse only if no ad is served'),
        2 => $this
          ->t('Expand only if an ad is served'),
      ],
      '#description' => $this
        ->t('<dl><dt>Never</dt><dd>Never collapse ad slots.</dd><dt>Collapse only</dt><dd>Collapse before any ad is loaded. Useful if ad slots will get filled most of the time.</dd><dt>Expand only</dt><dd>Collapse all divs on the page before any ads are fetched and expand if an ad is loaded into the ad slot. Useful if ad slots will stay empty most of the time.</dd></dl>'),
    ];
    $form['global_display_options']['hide_slug'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Hide slug if no ad is served (recommended)'),
      '#default_value' => $config
        ->get('hide_slug'),
      '#states' => [
        'visible' => [
          'input[name="collapse_empty_divs"]' => [
            '!value' => 0,
          ],
        ],
      ],
    ];

    // Global targeting options.
    $form['targeting_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Global Targeting'),
      '#open' => TRUE,
    ];
    $existing_targeting = $this
      ->getExistingTargeting($form_state, $config
      ->get('targeting'));
    $this
      ->addTargetingForm($form['targeting_settings'], $existing_targeting);

    // AdTest Settings.
    $form['adtest'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Ad Test Settings'),
      '#open' => TRUE,
    ];
    $form['adtest']['adtest_adunit_pattern'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Ad Unit Pattern for Ad Tests'),
      '#description' => $this
        ->t('Override the Ad Unit value for all the ad tags on a page by adding ?adtest=true to the URL. Use the tokens below to define how the ad unit should display. Example: [dfp_tag:network_id]/test/[dfp_tag:slot]'),
      '#default_value' => $config
        ->get('adtest_adunit_pattern'),
    ];

    // @todo Add back token browser.
    // @todo Drupal 7 also had the ability to inject Javascript. Discuss whether
    //   or not this should be implemented.
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('click_url') && $form_state
      ->getValue('async_rendering')) {
      $form_state
        ->setErrorByName('click_url', $this
        ->t('Setting a click URL does not work with async rendering.'));
    }
    if (preg_match(TagInterface::ADUNIT_PATTERN_VALIDATION_REGEX, $form_state
      ->getValue('adunit_pattern'))) {
      $form_state
        ->setErrorByName('adunit_pattern', $this
        ->t('Ad Unit Patterns can only include letters, numbers, hyphens, dashes, periods, slashes and tokens.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    if (!$values['async_rendering']) {
      $values['disable_init_load'] = FALSE;
    }
    $this
      ->config('dfp.settings')
      ->set('network_id', $values['network_id'])
      ->set('adunit_pattern', $values['adunit_pattern'])
      ->set('click_url', $values['click_url'])
      ->set('async_rendering', $values['async_rendering'])
      ->set('disable_init_load', $values['disable_init_load'])
      ->set('single_request', $values['single_request'])
      ->set('default_slug', $values['default_slug'])
      ->set('collapse_empty_divs', $values['collapse_empty_divs'])
      ->set('adtest_adunit_pattern', $values['adtest_adunit_pattern'])
      ->set('targeting', $values['targeting'])
      ->set('hide_slug', $values['hide_slug'])
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdminSettings::$bundleInfo protected property Entity bundle information.
AdminSettings::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AdminSettings::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
AdminSettings::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AdminSettings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AdminSettings::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AdminSettings::validateForm public function Form validation handler. Overrides FormBase::validateForm
AdminSettings::__construct public function Constructs a \Drupal\dfp\Form\AdminSettings object. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TargetingFormTrait::addTargetForm protected function Helper form builder for an individual target.
TargetingFormTrait::addTargetingForm protected function Helper form builder for the targeting form.
TargetingFormTrait::getExistingTargeting protected function Returns the current targets.
TargetingFormTrait::moreTargetsJs public function Ajax callback for adding targets to the targeting form.
TargetingFormTrait::targetFormValidate public static function Validation function used by an individual target in the targeting form.
TargetingFormTrait::targetingFormMoreTargetsSubmit public function Submit handler to add more targets to an ad tag.
TargetingFormTrait::targetingFormValidate public static function Validation function used by the targeting form.
TargetingFormTrait::trimTargetingValues protected static function Helper function that removes empty targets form form values.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.