5 * Contains \Drupal\linkit\Form\Attribute\OverviewForm.
8 namespace Drupal\linkit\Form\Attribute;
10 use Drupal\Core\Form\FormBase;
11 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\linkit\AttributeManager;
14 use Drupal\linkit\ConfigurableAttributeInterface;
15 use Drupal\linkit\ProfileInterface;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
19 * Provides an overview form for attribute on a profile.
21 class OverviewForm extends FormBase {
24 * The profiles to which the attributes are applied to.
26 * @var \Drupal\linkit\ProfileInterface
28 private $linkitProfile;
31 * The attribute manager.
33 * @var \Drupal\linkit\AttributeManager
38 * Constructs a new OverviewForm.
40 * @param \Drupal\linkit\AttributeManager $manager
41 * The attribute manager.
43 public function __construct(AttributeManager $manager) {
44 $this->manager = $manager;
50 public static function create(ContainerInterface $container) {
52 $container->get('plugin.manager.linkit.attribute')
59 public function getFormId() {
60 return "linkit_attribute_overview_form";
66 public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
67 $this->linkitProfile = $linkit_profile;
72 $this->t('Attribute'),
73 $this->t('Description'),
75 $this->t('Operations'),
77 '#empty' => $this->t('No attributes added.'),
81 'relationship' => 'sibling',
82 'group' => 'plugin-order-weight',
87 foreach ($this->linkitProfile->getAttributes() as $plugin) {
88 $key = $plugin->getPluginId();
90 $form['plugins'][$key]['#attributes']['class'][] = 'draggable';
91 $form['plugins'][$key]['#weight'] = $plugin->getWeight();
93 $form['plugins'][$key]['label'] = [
94 '#plain_text' => (string) $plugin->getLabel(),
97 $form['plugins'][$key]['description'] = [
98 '#plain_text' => (string) $plugin->getDescription(),
101 $form['plugins'][$key]['weight'] = [
103 '#title' => t('Weight for @title', ['@title' => (string) $plugin->getLabel()]),
104 '#title_display' => 'invisible',
105 '#default_value' => $plugin->getWeight(),
106 '#attributes' => ['class' => ['plugin-order-weight']],
109 $form['plugins'][$key]['operations'] = [
110 '#type' => 'operations',
114 $is_configurable = $plugin instanceof ConfigurableAttributeInterface;
115 if ($is_configurable) {
116 $form['plugins'][$key]['operations']['#links']['edit'] = [
117 'title' => t('Edit'),
118 'url' => Url::fromRoute('linkit.attribute.edit', [
119 'linkit_profile' => $this->linkitProfile->id(),
120 'plugin_instance_id' => $key,
125 $form['plugins'][$key]['operations']['#links']['delete'] = [
126 'title' => t('Delete'),
127 'url' => Url::fromRoute('linkit.attribute.delete', [
128 'linkit_profile' => $this->linkitProfile->id(),
129 'plugin_instance_id' => $key,
134 $form['actions'] = ['#type' => 'actions'];
135 $form['actions']['submit'] = [
137 '#value' => $this->t('Save'),
138 '#button_type' => 'primary',
147 public function submitForm(array &$form, FormStateInterface $form_state) {
148 foreach ($form_state->getValue('plugins') as $id => $plugin_data) {
149 if ($this->linkitProfile->getAttributes()->has($id)) {
150 $this->linkitProfile->getAttribute($id)->setWeight($plugin_data['weight']);
153 $this->linkitProfile->save();