public function HierarchyChildrenForm::form in Entity Reference Hierarchy 3.x
Same name and namespace in other branches
- 8.2 src/Form/HierarchyChildrenForm.php \Drupal\entity_hierarchy\Form\HierarchyChildrenForm::form()
Gets the actual form array to be built.
Overrides ContentEntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ HierarchyChildrenForm.php, line 84
Class
- HierarchyChildrenForm
- Defines a form for re-ordering children.
Namespace
Drupal\entity_hierarchy\FormCode
public function form(array $form, FormStateInterface $form_state) {
$cache = (new CacheableMetadata())
->addCacheableDependency($this->entity);
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields */
$fields = $this->parentCandidate
->getCandidateFields($this->entity);
if (!$fields) {
throw new NotFoundHttpException();
}
$fieldName = $form_state
->getValue('fieldname') ?: reset($fields);
if (count($fields) === 1) {
$form['fieldname'] = [
'#type' => 'value',
'#value' => $fieldName,
];
}
else {
$form['select_field'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'container-inline',
],
],
];
$form['select_field']['fieldname'] = [
'#type' => 'select',
'#title' => $this
->t('Field'),
'#description' => $this
->t('Field to reorder children in.'),
'#options' => array_map(function ($field_name) {
return $this->entity
->getFieldDefinitions()[$field_name]
->getLabel();
}, $fields),
'#default_value' => $fieldName,
];
$form['select_field']['update'] = [
'#type' => 'submit',
'#value' => $this
->t('Update'),
'#submit' => [
'::updateField',
],
];
}
/** @var \PNX\NestedSet\Node[] $children */
/** @var \PNX\NestedSet\NestedSetInterface $storage */
$storage = $this->nestedSetStorageFactory
->get($fieldName, $this->entity
->getEntityTypeId());
$children = $storage
->findChildren($this->nodeKeyFactory
->fromEntity($this->entity));
$childEntities = $this->entityTreeNodeMapper
->loadAndAccessCheckEntitysForTreeNodes($this->entity
->getEntityTypeId(), $children, $cache);
$form_state
->setTemporaryValue(self::CHILD_ENTITIES_STORAGE, $childEntities);
$form['#attached']['library'][] = 'entity_hierarchy/entity_hierarchy.nodetypeform';
$form['children'] = [
'#type' => 'table',
'#header' => [
t('Child'),
t('Type'),
t('Weight'),
t('Operations'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'children-order-weight',
],
],
'#empty' => $this
->t('There are no children to reorder'),
];
$bundles = FALSE;
foreach ($children as $weight => $node) {
if (!$childEntities
->contains($node)) {
// Doesn't exist or is access hidden.
continue;
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $childEntity */
$childEntity = $childEntities
->offsetGet($node);
if (!$childEntity
->isDefaultRevision()) {
// We only update default revisions here.
continue;
}
$child = $node
->getId();
$form['children'][$child]['#attributes']['class'][] = 'draggable';
$form['children'][$child]['#weight'] = $weight;
$form['children'][$child]['title'] = $childEntity
->toLink()
->toRenderable();
if (!$bundles) {
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($childEntity
->getEntityTypeId());
}
$form['children'][$child]['type'] = [
'#markup' => $bundles[$childEntity
->bundle()]['label'],
];
$form['children'][$child]['weight'] = [
'#type' => 'weight',
'#delta' => 50,
'#title' => t('Weight for @title', [
'@title' => $childEntity
->label(),
]),
'#title_display' => 'invisible',
'#default_value' => $childEntity->{$fieldName}->weight,
// Classify the weight element for #tabledrag.
'#attributes' => [
'class' => [
'children-order-weight',
],
],
];
// Operations column.
$form['children'][$child]['operations'] = [
'#type' => 'operations',
'#links' => [],
];
if ($childEntity
->access('update') && $childEntity
->hasLinkTemplate('edit-form')) {
$form['children'][$child]['operations']['#links']['edit'] = [
'title' => t('Edit'),
'url' => $childEntity
->toUrl('edit-form'),
];
}
if ($childEntity
->access('delete') && $childEntity
->hasLinkTemplate('delete-form')) {
$form['children'][$child]['operations']['#links']['delete'] = [
'title' => t('Delete'),
'url' => $childEntity
->toUrl('delete-form'),
];
}
}
$cache
->applyTo($form);
return $form;
}