public function AmpMetadataForm::form in Accelerated Mobile Pages (AMP) 8
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ AmpMetadataForm.php, line 105
Class
- AmpMetadataForm
- Class AmpMetadataForm.
Namespace
Drupal\amp\FormCode
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var AmpMetadata $amp_metadata */
$amp_metadata = $this->entity;
// Check if global metadata settings already exist.
$amp_metadata_global_exists = $this->ampMetadataInfo
->ampMetadataHasGlobal();
// Get AMP-enabled node types without existing settings.
$node_type_options = $this->ampMetadataInfo
->getAmpNodeTypesWithoutMetadataSettings();
if (!$this->moduleHandler
->moduleExists('token')) {
// Provide message in case somebody has upgraded AMP module but has not
// installed Token.
drupal_set_message($this
->t('The AMP module now requires the <a href="@module">Token</a> module as a dependency. Please download and install Token in order for AMP metadata to appear properly.', [
'@module' => 'https://www.drupal.org/project/token',
]), 'warning');
}
$introduction_title = $amp_metadata_global_exists && !$amp_metadata
->isGlobal() ? $this
->t('Content type override for metadata settings') : $this
->t('Global metadata settings');
$top_stories_guidelines_url = Url::fromUri('https://developers.google.com/search/docs/data-types/articles#article_types');
$form['introduction'] = [
'#type' => 'item',
'#title' => $introduction_title,
'#description' => t('These metadata settings provide information used in the Top Stories carousel in Google Search. Find complete details in the <a href=":top_stories_guidelines">Top Stories guidelines</a>.', [
':top_stories_guidelines' => $top_stories_guidelines_url
->toString(),
]),
];
// Show a node type selector if this is new metadata, and global metadata
// settings already exist.
if ($amp_metadata
->isNew() && $amp_metadata_global_exists) {
$form['node_type'] = array(
'#type' => 'select',
'#title' => t('Content type'),
'#description' => t('Select a content type for which you would like to override the global AMP metadata setting. Settings are overridden on a field by field basis.'),
'#options' => $node_type_options,
'#required' => TRUE,
'#default_value' => NULL,
);
}
// Add a collapsible section for organization information. Open by default
// for global settings.
$show_organization_fields = $amp_metadata_global_exists && !$amp_metadata
->isGlobal() ? FALSE : TRUE;
$form['organization_group'] = [
'#type' => 'details',
'#title' => 'Organization information',
'#open' => $show_organization_fields,
];
$form['organization_group']['description'] = [
'#type' => 'item',
'#description' => t('Provide information about your organization for use in search metadata.'),
];
// Add the token browser for ease in selecting token values.
$form['organization_group']['token_link'] = [
'#theme' => 'token_tree_link',
'#token_types' => 'all',
'#global_types' => TRUE,
'#click_insert' => TRUE,
'#show_restricted' => FALSE,
'#recursion_limit' => 3,
'#text' => t('Browse available tokens'),
];
// Add an option to set the organization name that appears in the carousel.
$org_name = $amp_metadata
->getOrganizationName();
$form['organization_group']['amp_organization_name'] = [
'#type' => 'textfield',
'#title' => t('Organization name'),
'#description' => t('The name of the organization that will appear in the Top Stories carousel. Tokens are allowed.'),
'#default_value' => $org_name ? $org_name : '',
'#attributes' => [
'placeholder' => '[site:name]',
],
];
// Add an option to upload an organizational logo for the carousel.
$top_stories_logo_guidelines_url = Url::fromUri('https://developers.google.com/search/docs/data-types/articles#amp-logo-guidelines');
$logo_config_fid = $amp_metadata
->getOrganizationLogoFid();
$default_logo_fid = $logo_config_fid ? [
$logo_config_fid,
] : NULL;
$form['organization_group']['amp_organization_logo_fid_new'] = [
'#type' => 'managed_file',
'#title' => t('Organization logo'),
'#description' => t('Upload a logo for your organization that will appear in the Top Stories carousel. <span class="warning">This logo must have a height of 60px and a width less than 600px. SVG logos are not allowed: please provide a JPG, JPEG, GIF or PNG. See the AMP <a href=":logo_guidelines">logo guidelines</a>.</span>', [
':logo_guidelines' => $top_stories_logo_guidelines_url
->toString(),
]),
'#default_value' => $default_logo_fid,
'#upload_location' => 'public://amp',
'#upload_validators' => [
'file_validate_is_image' => [],
'file_validate_extensions' => [
'png gif jpg jpeg',
],
'file_validate_image_resolution' => [
'600x60',
],
],
];
// Store the initial logo file ID. This will help us determine if the logo
// file is removed, in which case we should delete the file.
$form['organization_group']['amp_organization_logo_fid_previous'] = [
'#type' => 'hidden',
'#value' => $default_logo_fid,
];
// Add an option to select an image style for the organization logo.
$org_logo_style = $amp_metadata
->getOrganizationLogoImageStyleId();
$form['organization_group']['amp_organization_logo_image_style_id'] = [
'#type' => 'select',
'#title' => t('Organization logo image style'),
'#options' => image_style_options(TRUE),
'#description' => t('The image style to use for the organization logo.'),
'#default_value' => $org_logo_style ? $org_logo_style : '',
];
// Add a section for content information.
$form['content_group'] = [
'#type' => 'fieldset',
'#title' => 'Content information',
];
$form['content_group']['description'] = [
'#type' => 'item',
'#description' => t('Provide information about your content for use in the Top Stories carousel in Google Search.'),
];
// Add an option to select the schema type for AMP pages.
$schema_type_options = [
'Article' => 'Article',
'NewsArticle' => 'NewsArticle',
'BlogPosting' => 'BlogPosting',
];
$schema_type = $amp_metadata
->getContentSchemaType();
$form['content_group']['amp_content_schema_type'] = [
'#type' => 'select',
'#title' => t('AMP schema type'),
'#options' => $schema_type_options,
'#description' => t('The type of schema to use on AMP pages'),
'#default_value' => $schema_type ? $schema_type : 'NewsArticle',
];
// Add an option to set the headline on AMP pages.
$content_headline = $amp_metadata
->getContentHeadlineToken();
$form['content_group']['amp_content_headline'] = [
'#type' => 'textfield',
'#title' => t('Article headline'),
'#description' => t('A short headline for an AMP article, using fewer than 110 characters and no HTML markup. Use tokens to provide the correct headline for each article page.'),
'#default_value' => $content_headline ? $content_headline : '',
'#attributes' => [
'placeholder' => '[node:title]',
],
];
// Add an option to set the author on AMP pages.
$content_author = $amp_metadata
->getContentAuthorToken();
$form['content_group']['amp_content_author'] = [
'#type' => 'textfield',
'#title' => t('Author name'),
'#description' => t('The name of the author to use on AMP pages. Use tokens to provide the correct author for each article page. Token output should be text only, no HTML markup.'),
'#default_value' => $content_author ? $content_author : '',
'#attributes' => [
'placeholder' => '[node:author:display-name]',
],
];
// Add an option to set the description on AMP pages.
$content_description = $amp_metadata
->getContentDescriptionToken();
$form['content_group']['amp_content_description'] = [
'#type' => 'textfield',
'#title' => t('Article description'),
'#description' => t('A short description of an AMP article, using fewer than 150 characters and no HTML markup. Use tokens to provide the correct description for each article page.'),
'#default_value' => $content_description ? $content_description : '',
'#attributes' => [
'placeholder' => '[node:summary]',
],
];
// Add an option to set the image on AMP pages.
$top_stories_image_guidelines_url = Url::fromUri('https://developers.google.com/search/docs/data-types/articles#article_types');
$content_image = $amp_metadata
->getContentImageToken();
$form['content_group']['amp_content_image'] = [
'#type' => 'textfield',
'#title' => t('Article image for carousel'),
'#description' => t('An article image to appear in the Top Stories carousel. Images must be at least 696px wide: refer to <a href=":image_guidelines">article image guidelines</a> for further details. Use tokens to provide the correct image for each article page.', [
':image_guidelines' => $top_stories_image_guidelines_url
->toString(),
]),
'#default_value' => $content_image ? $content_image : '',
'#attributes' => [
'placeholder' => '[node:field_image]',
],
];
// Add an option to select an image style for the organization logo.
$content_image_style = $amp_metadata
->getContentImageStyleId();
$form['content_group']['amp_content_image_style_id'] = [
'#type' => 'select',
'#title' => t('Article image style'),
'#options' => image_style_options(TRUE),
'#description' => t('The image style to use for the article image'),
'#default_value' => $content_image_style ? $content_image_style : '',
];
// Add the token browser for ease in selecting token values.
$form['content_group']['token_link'] = [
'#theme' => 'token_tree_link',
'#token_types' => 'all',
'#global_types' => TRUE,
'#click_insert' => TRUE,
'#show_restricted' => FALSE,
'#recursion_limit' => 3,
'#text' => t('Browse available tokens'),
];
return $form;
}