class Tag in Doubleclick for Publishers (DFP) 8
Same name in this branch
- 8 src/Form/Tag.php \Drupal\dfp\Form\Tag
- 8 src/Entity/Tag.php \Drupal\dfp\Entity\Tag
Form to edit and add DFP tags.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\dfp\Form\Tag uses BreakpointFormTrait, TargetingFormTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of Tag
File
- src/
Form/ Tag.php, line 18 - Contains \Drupal\dfp\Form\Tag.
Namespace
Drupal\dfp\FormView source
class Tag extends EntityForm {
use BreakpointFormTrait;
use TargetingFormTrait;
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
// @todo Implement vertical tabs like D7 module.
// @todo Implement out_of_page setting like D7 module.
$form = parent::form($form, $form_state);
/** @var \Drupal\dfp\Entity\TagInterface $tag */
$tag = $this->entity;
if ($this->operation == 'add') {
$form['#title'] = $this
->t('Add DFP tag');
}
else {
$form['#title'] = $this
->t('Edit %label DFP tag', [
'%label' => $tag
->label(),
]);
}
// Tag settings.
$form['tag_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Tag Settings'),
'#open' => TRUE,
];
$form['tag_settings']['slot'] = [
'#type' => 'textfield',
'#title' => $this
->t('Ad Slot Name'),
'#required' => TRUE,
'#maxlength' => 64,
'#default_value' => $tag
->slot(),
'#description' => $this
->t('Example: leaderboard or box1'),
];
$form['tag_settings']['id'] = [
'#type' => 'machine_name',
'#maxlength' => 128,
'#default_value' => $tag
->id(),
'#description' => $this
->t('A unique machine-readable name for this DFP tag. Only use letters, numbers and underscores. Example: top_banner'),
'#machine_name' => [
'exists' => [
'Drupal\\dfp\\Entity\\Tag',
'load',
],
'source' => [
'tag_settings',
'slot',
],
],
];
$form['tag_settings']['size'] = [
'#type' => 'textfield',
'#title' => $this
->t('Size(s)'),
'#description' => $this
->t('Example: 300x600,300x250. For Out Of Page slots, use 0x0'),
'#required' => TRUE,
'#maxlength' => 64,
'#default_value' => $tag
->size(),
];
$form['tag_settings']['adunit'] = [
'#type' => 'textfield',
'#title' => $this
->t('Ad Unit Pattern'),
'#required' => FALSE,
'#maxlength' => 255,
'#default_value' => $tag
->adunit(),
'#description' => $this
->t('Use the tokens below to define how the ad unit should display. The network id will be included automatically. Example: [dfp_tag:url_parts:4]/[dfp_tag:slot]. Leave this field empty to use the default ad unit adunit as defined in <a href=":url">Global DFP Settings</a>.', [
':url' => Url::fromRoute('dfp.admin_settings')
->toString(),
]),
];
// @todo Add token browser.
// Global Display settings.
$form['tag_display_options'] = [
'#type' => 'details',
'#title' => $this
->t('Display Options'),
'#open' => TRUE,
];
$form['tag_display_options']['slug'] = [
'#type' => 'textfield',
'#title' => $this
->t('Slug'),
'#description' => $this
->t('Override the default slug for this ad tag. Use @none for no slug. Leave this field empty to use the default slug. Example: Advertisement', [
'@none' => '<none>',
]),
'#required' => FALSE,
'#maxlength' => 64,
'#default_value' => $tag
->slug(),
];
$form['tag_display_options']['block'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Create a block for this ad tag'),
'#description' => $this
->t('Display this ad in a block configurable. <a href=":url">Place the block</a>.', [
':url' => Url::fromRoute('block.admin_display')
->toString(),
]),
'#default_value' => $tag
->hasBlock(),
];
$form['tag_display_options']['short_tag'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Render this tag without javascript'),
'#description' => $this
->t('Use this option for ads included in emails.'),
'#default_value' => $tag
->shortTag(),
];
// Responsive settings.
$form['breakpoint_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Responsive Settings'),
'#open' => TRUE,
];
$existing_breakpoints = $this
->getExistingBreakpoints($form_state, $tag
->breakpoints());
$this
->addBreakpointsForm($form['breakpoint_settings'], $existing_breakpoints);
// Targeting options.
$form['targeting_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Targeting'),
'#open' => TRUE,
];
$existing_targeting = $this
->getExistingTargeting($form_state, $tag
->targeting());
$this
->addTargetingForm($form['targeting_settings'], $existing_targeting);
// Backfill ad settings options.
$form['adsense_backfill'] = [
'#type' => 'details',
'#title' => $this
->t('Backfill Ad Settings'),
'#open' => TRUE,
'#tree' => TRUE,
];
$form['adsense_backfill']['ad_types'] = [
'#type' => 'select',
'#title' => $this
->t('AdSense Ad Type'),
'#empty_option' => $this
->t('- None -'),
'#empty_value' => '',
'#default_value' => $tag
->adsenseAdTypes(),
'#options' => [
TagInterface::ADSENSE_TEXT_IMAGE => $this
->t('Both image and text ads'),
TagInterface::ADSENSE_IMAGE => $this
->t('Only image ads'),
TagInterface::ADSENSE_TEXT => $this
->t('Only text ads'),
],
'#description' => $this
->t('Choose what type of ads this tag can display when AdSense ads are used for backfill.'),
];
$form['adsense_backfill']['channel_ids'] = [
'#type' => 'textfield',
'#title' => $this
->t('AdSense Channel ID(s)'),
'#default_value' => $tag
->adsenseChannelIds(),
'#required' => FALSE,
'#description' => $this
->t('Example: 271828183+314159265'),
'#states' => [
'!visible' => [
[
':input[name="adsense_backfill[ad_types]"]' => [
'value' => '',
],
],
],
],
];
$form['adsense_backfill']['color'] = [
'#type' => 'fieldgroup',
'#title' => $this
->t('Color Settings for Text Ads'),
'#attributes' => [
'class' => [
'form-item',
],
],
'#states' => [
'visible' => [
[
':input[name="adsense_backfill[ad_types]"]' => [
'value' => TagInterface::ADSENSE_TEXT,
],
],
[
':input[name="adsense_backfill[ad_types]"]' => [
'value' => TagInterface::ADSENSE_TEXT_IMAGE,
],
],
],
],
];
$adsense_color_settings = [
'background' => $this
->t('Background color'),
'border' => $this
->t('Border color'),
'link' => $this
->t('Link color'),
'text' => $this
->t('Text color'),
'url' => $this
->t('URL color'),
];
foreach ($adsense_color_settings as $setting => $title) {
// @todo integrate color picker if color module enabled.
$form['adsense_backfill']['color'][$setting] = [
'#type' => 'textfield',
'#title' => $title,
'#attributes' => [
'class' => [
'color-setting',
],
],
'#field_prefix' => '#',
'#default_value' => $tag
->adsenseColor($setting),
'#size' => 6,
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (preg_match(TagInterface::ADUNIT_PATTERN_VALIDATION_REGEX, $form_state
->getValue('adunit'))) {
$form_state
->setErrorByName('adunit', $this
->t('Ad Unit Patterns can only include letters, numbers, hyphens, dashes, periods, slashes and tokens.'));
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$tag = $this->entity;
$status = $tag
->save();
$t_args['%slot'] = $tag
->label();
if ($status == SAVED_UPDATED) {
$this
->messenger()
->addMessage(t('The DFP tag %slot has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
$this
->messenger()
->addMessage(t('The DFP tag %slot has been added.', $t_args));
$context = array_merge($t_args, [
'link' => $tag
->toLink($this
->t('Edit DFP tag'), 'edit-form')
->toString(),
]);
$this
->logger('dfp')
->notice('Added DFP tag %slot.', $context);
}
$form_state
->setRedirectUrl($tag
->toUrl('collection'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BreakpointFormTrait:: |
protected | function | Helper form builder for an individual breakpoint. | |
BreakpointFormTrait:: |
protected | function | Helper form builder for the breakpoints form. | |
BreakpointFormTrait:: |
public static | function | Validation function used by an individual breakpoint. | |
BreakpointFormTrait:: |
public static | function | Validation function used by the breakpoints form. | |
BreakpointFormTrait:: |
protected static | function | Helper function that removes empty breakpoints from form values. | |
BreakpointFormTrait:: |
protected | function | Returns the current breakpoints. | |
BreakpointFormTrait:: |
public | function | Ajax callback for adding breakpoints to the breakpoint form. | |
BreakpointFormTrait:: |
public | function | Submit handler to add more breakpoints to an ad tag. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
Tag:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
Tag:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
Tag:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
TargetingFormTrait:: |
protected | function | Helper form builder for an individual target. | |
TargetingFormTrait:: |
protected | function | Helper form builder for the targeting form. | |
TargetingFormTrait:: |
protected | function | Returns the current targets. | |
TargetingFormTrait:: |
public | function | Ajax callback for adding targets to the targeting form. | |
TargetingFormTrait:: |
public static | function | Validation function used by an individual target in the targeting form. | |
TargetingFormTrait:: |
public | function | Submit handler to add more targets to an ad tag. | |
TargetingFormTrait:: |
public static | function | Validation function used by the targeting form. | |
TargetingFormTrait:: |
protected static | function | Helper function that removes empty targets form form values. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |