class BaseSettingsForm in Facebook Instant Articles 8.2
Same name and namespace in other branches
- 3.x src/Form/BaseSettingsForm.php \Drupal\fb_instant_articles\Form\BaseSettingsForm
Facebook Instant Articles base settings form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\fb_instant_articles\Form\BaseSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of BaseSettingsForm
1 string reference to 'BaseSettingsForm'
File
- src/
Form/ BaseSettingsForm.php, line 14
Namespace
Drupal\fb_instant_articles\FormView source
class BaseSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'fb_instant_articles.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'base_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('fb_instant_articles.settings');
// Add the page id configuration.
$args = [
'@claim_url' => 'https://developers.facebook.com/docs/instant-articles/claim-url',
];
$form['page_id'] = [
'#title' => t('Facebook Page ID'),
'#type' => 'textfield',
'#default_value' => $config
->get('page_id'),
'#required' => TRUE,
'#description' => $this
->t('In order to designate the domain that will host your articles you must add your Facebook page ID to a metatag in the HEAD tag of your HTML page. Entering your Facebook Page ID here will add the metatag automatically. See <a href="@claim_url">Claiming your URL</a>.', $args),
];
// Add the style configuration.
$form['style'] = [
'#type' => 'textfield',
'#title' => t('Article Style'),
'#default_value' => $config
->get('style') ? $config
->get('style') : 'default',
'#size' => 30,
'#required' => TRUE,
'#description' => $this
->t('Assign your Instant Articles a custom style. To begin, customize a template using the <a href="@style_url" target="_blank">Style Editor</a>. Next, input the name of the style above. <strong>Note</strong>: if this field is left blank, the module will enable the “Default” style. Learn more about Instant Articles style options in the <a href="@design_url" target="_blank">Design Guide</a>.', [
'@style_url' => '',
'@design_url' => 'https://developers.facebook.com/docs/instant-articles/guides/design',
]),
];
// Add the Ads sub-section.
$form['ads'] = [
'#type' => 'details',
'#title' => t('Ads'),
'#open' => TRUE,
'#description' => t('Choose your preferred method for displaying ads in your Instant Articles and input the code in the boxes below. Learn more about your options for <a href="@ads_url">advertising in Instant Articles</a>', [
'@ads_url' => 'https://developers.facebook.com/docs/instant-articles/ads-analytics',
]),
];
$form['ads']['ads_type'] = [
'#type' => 'select',
'#title' => t('Ad Type'),
'#default_value' => $config
->get('ads.type') ? $config
->get('ads.type') : AdTypes::AD_TYPE_NONE,
'#options' => [
AdTypes::AD_TYPE_NONE => t('None'),
AdTypes::AD_TYPE_FBAN => t('Facebook Audience Network'),
AdTypes::AD_TYPE_SOURCE_URL => t('Source URL'),
AdTypes::AD_TYPE_EMBED_CODE => t('Embed Code'),
],
'#description' => t('<strong>Note:</strong> this module will automatically place the ads within your articles.'),
'#attributes' => [
'class' => [
'ad-type',
],
],
];
$form['ads']['ads_iframe_url'] = [
'#type' => 'textfield',
'#title' => t('Ad Source URL'),
'#default_value' => $config
->get('ads.iframe_url'),
'#description' => t("<strong>Note:</strong> Instant Articles only supports Direct Sold ads. No programmatic ad networks, other than Facebook's Audience Network, are permitted."),
'#size' => 80,
'#element_validate' => [
[
$this,
'validateAdSourceUrl',
],
],
'#states' => [
'visible' => [
'[name=ads_type]' => [
'value' => AdTypes::AD_TYPE_SOURCE_URL,
],
],
],
];
$form['ads']['ads_an_placement_id'] = [
'#type' => 'textfield',
'#title' => t('Audience Network Placement ID'),
'#default_value' => $config
->get('ads.an_placement_id'),
'#description' => t('Find your <a href="@placement_id_url" target="_blank">Placement ID</a> on your app\'s <a href="@audience_network_url" target="_blank">Audience Network Portal</a>.', [
'@placement_id_url' => '',
'@audience_netowrk_url' => '',
]),
'#size' => 30,
'#element_validate' => [
[
$this,
'validateAnPlacementId',
],
],
'#states' => [
'visible' => [
'[name=ads_type]' => [
'value' => AdTypes::AD_TYPE_FBAN,
],
],
],
];
$form['ads']['ads_embed_code'] = [
'#type' => 'textarea',
'#title' => t('Ad Embed Code'),
'#default_value' => $config
->get('ads.embed_code'),
'#description' => t('Add code to be used for displayed ads in your Instant Articles.'),
'#size' => 30,
'#element_validate' => [
[
$this,
'validateAdEmbedCode',
],
],
'#states' => [
'visible' => [
'[name=ads_type]' => [
'value' => AdTypes::AD_TYPE_EMBED_CODE,
],
],
],
];
$form['ads']['ads_dimensions'] = [
'#type' => 'select',
'#title' => t('Ad Dimensions'),
'#options' => [
'300x250' => t('Large (300 x 250)'),
],
'#default_value' => $config
->get('ads.dimensions'),
'#states' => [
'invisible' => [
'[name=ads_type]' => [
'value' => AdTypes::AD_TYPE_NONE,
],
],
],
];
// Add the Analytics sub-section.
$form['analytics'] = [
'#type' => 'details',
'#title' => t('Analytics'),
'#open' => TRUE,
'#description' => t('Enable 3rd-party analytics to be used with Instant Articles. You can use an embed code to insert your own trackers and analytics. Learn more about <a href="@analytics_url">analytics in Instant Articles</a>.', [
'@analytics_url' => 'https://developers.facebook.com/docs/instant-articles/ads-analytics#analytics',
]),
];
$form['analytics']['analytics_embed_code'] = [
'#type' => 'textarea',
'#title' => t('Analytics Embed Code'),
'#default_value' => $config
->get('analytics.embed_code'),
'#description' => t('Add code for any analytics services you wish to use. <strong>Note:</strong> you do not need to include any <op-tracker> tags. The module will automatically include them in the article markup.'),
'#size' => 30,
];
// Add the Debug Configuration.
$form['transformer_logging_level'] = [
'#type' => 'select',
'#options' => [
TransformerLog::OFF => $this
->t('Off'),
TransformerLog::ERROR => $this
->t('Error'),
TransformerLog::INFO => $this
->t('Info'),
TransformerLog::DEBUG => $this
->t('Debug'),
],
'#title' => $this
->t('Transformer logging level'),
'#default_value' => $config
->get('transformer_logging_level') ? $config
->get('transformer_logging_level') : TransformerLog::ERROR,
'#description' => $this
->t('The FBIA Transformer outputs logs as it transforms HTML into the Facebook Instant Articles format. This setting allows you to capture those logs in Drupal logs for debugging purposes.'),
];
// Add the Canonical URL override.
$form['canonical_url_override'] = [
'#type' => 'textfield',
'#title' => t('Canonical URL override'),
'#default_value' => $config
->get('canonical_url_override', ''),
'#description' => t('If you need to override the base url portion of the canonical URL, you can do so here. This may be helpful for development domains or necessary if admin users perform tasks that trigger Facebook requests from alternate domains. This URL should not include a trailing slash (e.g. http://drupal.org).'),
];
return parent::buildForm($form, $form_state);
}
/**
* Validate the Ad Source Url field.
*
* @param array $form
* FAPI array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*/
public function validateAdSourceUrl(array &$form, FormStateInterface $form_state) {
// Only validate if Source URL is selected as ad type.
if ($form_state
->getValue('ads_type') != AdTypes::AD_TYPE_SOURCE_URL) {
return;
}
$ads_iframe_url = $form_state
->getValue('ads_iframe_url');
if (empty($ads_iframe_url)) {
$form_state
->setErrorByName('ads_iframe_url', $this
->t('You must specify a valid source URL for your Ads when using the Source URL ad type.'));
}
if (!UrlHelper::isValid($ads_iframe_url, TRUE)) {
$form_state
->setErrorByName('ads_iframe_url', $this
->t('You must specify a valid source URL for your Ads when using the Source URL ad type.'));
}
}
/**
* Validate the Audience Network placement id.
*
* @param array $form
* FAPI array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*/
public function validateAnPlacementId(array &$form, FormStateInterface $form_state) {
// Only validate if Audience Network is selected as ad type.
if ($form_state
->getValue('ads_type') != AdTypes::AD_TYPE_FBAN) {
return;
}
$ads_an_placement_id = $form_state
->getValue('ads_an_placement_id');
if (empty($ads_an_placement_id)) {
$form_state
->setErrorByName('ads_an_placement_id', $this
->t('You must specify a valid Placement ID when using the Audience Network ad type.'));
}
if (preg_match('/^[\\d_]+$/', $ads_an_placement_id) !== 1) {
$form_state
->setErrorByName('ads_an_placement_id', $this
->t('You must specify a valid Placement ID when using the Audience Network ad type. To find or set your placement id, you will need to go to your Audience Network account for Instant Articles. In the account, navigate to ‘Placements’ and create a ‘Placement of Banner’ type. You will only need one placement.'));
}
}
/**
* Validate the Ad Embed code.
*
* @param array $form
* FAPI array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*/
public function validateAdEmbedCode(array &$form, FormStateInterface $form_state) {
// Only validate if Embed Code is selected as ad type.
if ($form_state
->getValue('ads_type') != AdTypes::AD_TYPE_EMBED_CODE) {
return;
}
$ads_embed_code = $form_state
->getValue('ads_embed_code');
if (empty($ads_embed_code)) {
$form_state
->setErrorByName('ads_embed_code', $this
->t('You must specify Embed Code for your Ads when using the Embed Code ad type.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('fb_instant_articles.settings')
->set('page_id', $form_state
->getValue('page_id'))
->set('style', $form_state
->getValue('style'))
->set('ads.type', $form_state
->getValue('ads_type'))
->set('ads.iframe_url', $form_state
->getValue('ads_iframe_url'))
->set('ads.an_placement_id', $form_state
->getValue('ads_an_placement_id'))
->set('ads.embed_code', $form_state
->getValue('ads_embed_code'))
->set('ads.dimensions', $form_state
->getValue('ads_dimensions'))
->set('analytics.embed_code', $form_state
->getValue('analytics_embed_code'))
->set('transformer_logging_level', $form_state
->getValue('transformer_logging_level'))
->set('canonical_url_override', $form_state
->getValue('canonical_url_override'))
->save();
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BaseSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
BaseSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
BaseSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
BaseSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
BaseSettingsForm:: |
public | function | Validate the Ad Embed code. | |
BaseSettingsForm:: |
public | function | Validate the Ad Source Url field. | |
BaseSettingsForm:: |
public | function | Validate the Audience Network placement id. | |
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
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 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. | |
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. |