5 * Contains \Drupal\linkit\Form\Attribute\AddForm.
8 namespace Drupal\linkit\Form\Attribute;
10 use Drupal\Core\Form\FormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\linkit\AttributeManager;
13 use Drupal\linkit\ConfigurableAttributeInterface;
14 use Drupal\linkit\ProfileInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Provides a form to apply attributes to a profile.
20 class AddForm extends FormBase {
23 * The profiles to which the attributes will be applied.
25 * @var \Drupal\linkit\ProfileInterface
27 protected $linkitProfile;
30 * The attribute manager.
32 * @var \Drupal\linkit\AttributeManager
37 * Constructs a new AddForm.
39 * @param \Drupal\linkit\AttributeManager $manager
40 * The attribute manager.
42 public function __construct(AttributeManager $manager) {
43 $this->manager = $manager;
49 public static function create(ContainerInterface $container) {
51 $container->get('plugin.manager.linkit.attribute')
58 public function getFormId() {
59 return "linkit_attribute_add_form";
65 public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
66 $this->linkitProfile = $linkit_profile;
68 $form['#attached']['library'][] = 'linkit/linkit.admin';
70 'label' => $this->t('Attributes'),
71 'description' => $this->t('Description'),
75 '#type' => 'tableselect',
77 '#options' => $this->buildRows(),
78 '#empty' => $this->t('No attributes available.'),
82 $form['actions'] = ['#type' => 'actions'];
83 $form['actions']['submit'] = [
85 '#value' => $this->t('Save and continue'),
86 '#submit' => ['::submitForm'],
87 '#tableselect' => TRUE,
88 '#button_type' => 'primary',
97 public function validateForm(array &$form, FormStateInterface $form_state) {
98 if (empty($form_state->getValue('plugin'))) {
99 $form_state->setErrorByName('plugin', $this->t('No attribute selected.'));
106 public function submitForm(array &$form, FormStateInterface $form_state) {
107 $form_state->cleanValues();
109 /** @var \Drupal\linkit\AttributeInterface $plugin */
110 $plugin = $this->manager->createInstance($form_state->getValue('plugin'));
111 $plugin_id = $this->linkitProfile->addAttribute($plugin->getConfiguration());
112 $this->linkitProfile->save();
114 $this->logger('linkit')->notice('Added %label attribute to the @profile profile.', [
115 '%label' => $this->linkitProfile->getAttribute($plugin_id)->getLabel(),
116 '@profile' => $this->linkitProfile->label(),
119 $is_configurable = $plugin instanceof ConfigurableAttributeInterface;
120 if ($is_configurable) {
121 $form_state->setRedirect('linkit.attribute.edit', [
122 'linkit_profile' => $this->linkitProfile->id(),
123 'plugin_instance_id' => $plugin_id,
127 drupal_set_message($this->t('Added %label attribute.', ['%label' => $plugin->getLabel()]));
129 $form_state->setRedirect('linkit.attributes', [
130 'linkit_profile' => $this->linkitProfile->id(),
136 * Builds the table rows.
138 * Only attributes that is not already applied to the profile are shown.
141 * An array of table rows.
143 private function buildRows() {
146 $applied_plugins = $this->linkitProfile->getAttributes()->getConfiguration();
147 $all_plugins = $this->manager->getDefinitions();
148 uasort($all_plugins, function ($a, $b) {
149 return strnatcasecmp($a['label'], $b['label']);
151 foreach (array_diff_key($all_plugins, $applied_plugins) as $definition) {
152 /** @var \Drupal\linkit\AttributeInterface $plugin */
153 $plugin = $this->manager->createInstance($definition['id']);
156 'label' => (string) $plugin->getLabel(),
157 'description' => (string) $plugin->getDescription(),
160 $rows[$plugin->getPluginId()] = $row;