public function PatternEditForm::buildForm in View Mode Page 3.2.x
Same name and namespace in other branches
- 8.3 src/Form/PatternEditForm.php \Drupal\view_mode_page\Form\PatternEditForm::buildForm()
- 4.0.x src/Form/PatternEditForm.php \Drupal\view_mode_page\Form\PatternEditForm::buildForm()
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 EntityForm::buildForm
File
- src/
Form/ PatternEditForm.php, line 99
Class
- PatternEditForm
- Edit form for view_mode_page patterns.
Namespace
Drupal\view_mode_page\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $this->entity
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#title' => $this
->t('ID'),
'#maxlength' => 255,
'#default_value' => $this->entity
->id(),
'#required' => TRUE,
'#disabled' => !$this->entity
->isNew(),
'#machine_name' => [
'exists' => 'Drupal\\view_mode_page\\Entity\\ViewmodepagePattern::load',
],
];
$form['pattern'] = [
'#type' => 'textfield',
'#title' => 'Path pattern',
'#default_value' => $this->entity
->getPattern(),
'#size' => 65,
'#maxlength' => 1280,
'#required' => TRUE,
'#description' => $this
->t('
<strong>Must include % for the regular entity url/alias placeholder</strong> (path patterns are always based on the regular entity url\'s/aliases).<br>
Example scenario:<br>
<em>Show a node as "teaser" when the entity url/alias has a suffix of "/summary".</em><br>
- Path pattern: "/%/summary".<br>
- Entity type: "Content".<br>
- View mode: "Teaser".<br>
- If you have a content page which regular entity url/alias is "/my/great/page" and "node/123".<br>
- You can now visit "/my/great/page/summary" and "/node/123/summary" which will render that node in the given view mode.'),
];
$options = [];
foreach ($this->manager
->getVisibleDefinitions() as $plugin_id => $plugin_definition) {
$options[$plugin_id] = $plugin_definition['label'];
}
$form['type'] = [
'#type' => 'select',
'#title' => $this
->t('Entity type'),
'#default_value' => $this->entity
->getType(),
'#options' => $options,
'#required' => TRUE,
'#limit_validation_errors' => [
[
'type',
],
],
'#submit' => [
'::submitSelectType',
],
'#executes_submit_callback' => TRUE,
'#ajax' => [
'callback' => '::ajaxReplacePatternForm',
'wrapper' => 'view_mode_page-pattern',
'method' => 'replace',
],
];
$form['pattern_container'] = [
'#type' => 'container',
'#prefix' => '<div id="view_mode_page-pattern">',
'#suffix' => '</div>',
];
// If there is no type yet, stop here.
if ($this->entity
->getType()) {
$alias_type = $this->entity
->getAliasType();
// Expose bundle and language conditions.
if ($alias_type
->getDerivativeId() && ($entity_type = $this->entityTypeManager
->getDefinition($alias_type
->getDerivativeId()))) {
$default_bundles = [];
$default_languages = [];
foreach ($this->entity
->getSelectionConditions() as $condition) {
if (in_array($condition
->getPluginId(), [
'entity_bundle:' . $entity_type
->id(),
'node_type',
])) {
$default_bundles = $condition
->getConfiguration()['bundles'];
}
elseif ($condition
->getPluginId() == 'language') {
$default_languages = $condition
->getConfiguration()['langcodes'];
}
}
if ($entity_type
->hasKey('bundle') && ($bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity_type
->id()))) {
$bundle_options = [];
foreach ($bundles as $id => $info) {
$bundle_options[$id] = $info['label'];
}
$form['pattern_container']['bundles'] = [
'#title' => $entity_type
->getBundleLabel(),
'#type' => 'checkboxes',
'#options' => $bundle_options,
'#default_value' => $default_bundles,
'#description' => $this
->t('Check to which types this pattern should be applied. Leave empty to allow any.'),
];
}
if ($this->languageManager
->isMultilingual() && $entity_type
->isTranslatable()) {
$language_options = [];
foreach ($this->languageManager
->getLanguages() as $id => $language) {
$language_options[$id] = $language
->getName();
}
$form['pattern_container']['languages'] = [
'#title' => $this
->t('Languages'),
'#type' => 'checkboxes',
'#options' => $language_options,
'#default_value' => $default_languages,
'#description' => $this
->t('Check to which languages this pattern should be applied. Leave empty to allow any.'),
];
}
$view_mode_options = $this->entityDisplayRepositoy
->getViewModeOptions($alias_type
->getDerivativeId());
$form['pattern_container']['view_mode'] = [
'#title' => $this
->t('View mode'),
'#type' => 'select',
'#options' => $view_mode_options,
'#default_value' => $this->entity
->getViewMode(),
'#required' => TRUE,
'#description' => $this
->t('The view mode for rendering the entity.'),
];
}
}
return parent::buildForm($form, $form_state);
}