Example #1
0
  /**
   * Test adding a configurable attribute to a profile.
   */
  function testAddConfigurable() {
    $this->drupalGet(Url::fromRoute('linkit.matcher.add', [
      'linkit_profile' => $this->linkitProfile->id(),
    ]));

    $edit = array();
    $edit['plugin'] = 'configurable_dummy_matcher';
    $this->drupalPostForm(NULL, $edit, t('Save and continue'));

    // Load the saved profile.
    $this->linkitProfile = Profile::load($this->linkitProfile->id());

    $matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
    /** @var \Drupal\linkit\MatcherInterface $plugin */
    $plugin = $this->linkitProfile->getMatcher(current($matcher_ids));

    $this->assertUrl(Url::fromRoute('linkit.matcher.edit', [
      'linkit_profile' => $this->linkitProfile->id(),
      'plugin_instance_id' => $plugin->getUuid(),
    ]));

    $this->drupalGet(Url::fromRoute('linkit.matchers', [
      'linkit_profile' => $this->linkitProfile->id(),
    ]));

    $this->assertNoText(t('No matchers added.'));
  }
  /**
   * Tests the attribute route title callback.
   */
  function testAttributeTitle() {
    /** @var \Drupal\linkit\AttributeInterface $plugin */
    $plugin = $this->container->get('plugin.manager.linkit.attribute')->createInstance('configurable_dummy_attribute');
    $this->linkitProfile->addAttribute($plugin->getConfiguration());
    $this->linkitProfile->save();

    $this->drupalGet(Url::fromRoute('linkit.attribute.edit', [
      'linkit_profile' => $this->linkitProfile->id(),
      'plugin_instance_id' => $plugin->getPluginId(),
    ]));
    $this->assertText('Edit ' . $plugin->getLabel() . ' attribute');
  }
Example #3
0
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->linkitProfile->removeMatcher($this->linkitMatcher);

    drupal_set_message($this->t('The matcher %label has been deleted.', ['%label' => $this->linkitMatcher->getLabel()]));
    $this->logger('linkit')->notice('The matcher %label has been deleted in the @profile profile.', [
      '%label' => $this->linkitMatcher->getLabel(),
      '@profile' => $this->linkitProfile->label(),
    ]);

    $form_state->setRedirect('linkit.matchers', [
      'linkit_profile' => $this->linkitProfile->id(),
    ]);

  }
Example #4
0
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->cleanValues();
    $plugin_data = (new FormState())->setValues($form_state->getValues());
    $this->linkitMatcher->submitConfigurationForm($form, $plugin_data);
    $this->linkitProfile->save();

    drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->linkitMatcher->getLabel())));
    $this->logger('linkit')->notice('The matcher %label has been updated in the @profile profile.', [
      '%label' => $this->linkitMatcher->getLabel(),
      '@profile' => $this->linkitProfile->label(),
    ]);

    $form_state->setRedirect('linkit.matchers', [
      'linkit_profile' => $this->linkitProfile->id(),
    ]);
  }
Example #5
0
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->cleanValues();

    /** @var \Drupal\linkit\MatcherInterface $plugin */
    $plugin = $this->manager->createInstance($form_state->getValue('plugin'));

    $plugin_uuid = $this->linkitProfile->addMatcher($plugin->getConfiguration());
    $this->linkitProfile->save();

    $this->logger('linkit')->notice('Added %label matcher to the @profile profile.', [
      '%label' => $this->linkitProfile->getMatcher($plugin_uuid)->getLabel(),
      '@profile' => $this->linkitProfile->label(),
    ]);

    $is_configurable = $plugin instanceof ConfigurableMatcherInterface;
    if ($is_configurable) {
      $form_state->setRedirect('linkit.matcher.edit', [
        'linkit_profile' => $this->linkitProfile->id(),
        'plugin_instance_id' => $plugin_uuid,
      ]);
    }
    else {
      drupal_set_message($this->t('Added %label matcher.', ['%label' => $plugin->getLabel()]));

      $form_state->setRedirect('linkit.matchers', [
        'linkit_profile' => $this->linkitProfile->id(),
      ]);
    }
  }
Example #6
0
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($this->linkitProfile->getAttributes()->has($this->linkitAttribute->getPluginId())) {
      $this->linkitProfile->removeAttribute($this->linkitAttribute->getPluginId());
      $this->linkitProfile->save();

      drupal_set_message($this->t('The attribute %label has been deleted.', ['%label' => $this->linkitAttribute->getLabel()]));
      $this->logger('linkit')->notice('The attribute %label has been deleted in the @profile profile.', [
        '%label' => $this->linkitAttribute->getLabel(),
        '@profile' => $this->linkitProfile->label(),
      ]);
    }

    $form_state->setRedirect('linkit.attributes', [
      'linkit_profile' => $this->linkitProfile->id(),
    ]);
  }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state) {
   foreach ($form_state->getValue('plugins') as $id => $plugin_data) {
     if ($this->linkitProfile->getMatchers()->has($id)) {
       $this->linkitProfile->getMatcher($id)->setWeight($plugin_data['weight']);
     }
   }
   $this->linkitProfile->save();
 }
Example #8
0
  /**
   * Gets the results.
   *
   * @param ProfileInterface $linkitProfile
   *   The linkit profile.
   * @param $search_string
   *   The string ro use in the matchers.
   *
   * @return array
   *   An array of matches.
   */
  public function getResults(ProfileInterface $linkitProfile, $search_string) {
    $matches = array();

    if (empty(trim($search_string))) {
      return [[
        'title' => t('No results'),
      ]];
    }

    // Special for link to front page.
    if (strpos($search_string, 'front') !== FALSE) {
      $matches[] = [
        'title' => t('Front page'),
        'description' => 'The front page for this site.',
        'path' => Url::fromRoute('<front>')->toString(),
        'group' => t('System'),
      ];
    }

    foreach ($linkitProfile->getMatchers() as $plugin) {
      $matches = array_merge($matches, $plugin->getMatches($search_string));
    }

    // Check for an e-mail address then return an e-mail match and create a
    // mail-to link if appropriate.
    if (filter_var($search_string, FILTER_VALIDATE_EMAIL)) {
      $matches[] = [
        'title' => t('E-mail @email', ['@email' => $search_string]),
        'description' => t('Opens your mail client ready to e-mail @email', ['@email' => $search_string]),
        'path' => 'mailto:' . Html::escape($search_string),
        'group' => t('E-mail'),
      ];
    }

    // If there is still no matches, return a "no results" array.
    if (empty($matches)) {
      return [[
        'title' => t('No results'),
      ]];
    }

    return $matches;
  }
Example #9
0
  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $linkit_profile = $this->entity;

    // Prevent leading and trailing spaces in linkit profile labels.
    $linkit_profile->set('label', trim($linkit_profile->label()));

    $status = $linkit_profile->save();
    $edit_link = $this->entity->link($this->t('Edit'));
    switch ($status) {
      case SAVED_NEW:
        drupal_set_message($this->t('Created new profile %label.', ['%label' => $linkit_profile->label()]));
        $this->logger('linkit')->notice('Created new profile %label.', ['%label' => $linkit_profile->label(), 'link' => $edit_link]);
        $form_state->setRedirect('linkit.matchers', [
          'linkit_profile' => $linkit_profile->id(),
        ]);
        break;

      case SAVED_UPDATED:
        drupal_set_message($this->t('Updated profile %label.', ['%label' => $linkit_profile->label()]));
        $this->logger('linkit')->notice('Updated profile %label.', ['%label' => $linkit_profile->label(), 'link' => $edit_link]);
        $form_state->setRedirectUrl($linkit_profile->urlInfo('edit-form'));
        break;
    }
  }
Example #10
0
  /**
   * {@inheritdoc}
   *
   * @param \Drupal\filter\Entity\FilterFormat $filter_format
   *   The filter format for which this dialog corresponds.
   */
  public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {
    // The default values are set directly from \Drupal::request()->request,
    // provided by the editor plugin opening the dialog.
    $user_input = $form_state->getUserInput();
    $input = isset($user_input['editor_object']) ? $user_input['editor_object'] : [];

    /** @var \Drupal\editor\EditorInterface $editor */
    $editor = $this->editorStorage->load($filter_format->id());
    $linkit_profile_id = $editor->getSettings()['plugins']['linkit']['linkit_profile'];
    $this->linkitProfile = $this->linkitProfileStorage->load($linkit_profile_id);

    $form['#tree'] = TRUE;
    $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
    $form['#prefix'] = '<div id="linkit-editor-dialog-form">';
    $form['#suffix'] = '</div>';

    // Everything under the "attributes" key is merged directly into the
    // generated link tag's attributes.
    $form['attributes']['href'] = [
      '#title' => $this->t('Link'),
      '#type' => 'linkit',
      '#default_value' => isset($input['href']) ? $input['href'] : '',
      '#description' => $this->t('Start typing to find content or paste a URL.'),
      '#autocomplete_route_name' => 'linkit.autocomplete',
      '#autocomplete_route_parameters' => [
        'linkit_profile_id' => $linkit_profile_id
      ],
      '#weight' => 0,
    ];

    $this->addAttributes($form, $form_state, $this->linkitProfile->getAttributes());

    $form['actions'] = [
      '#type' => 'actions',
    ];

    $form['actions']['save_modal'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#submit' => [],
      '#ajax' => [
        'callback' => '::submitForm',
        'event' => 'click',
      ],
    ];

    return $form;
  }
Example #11
0
  /**
   * Builds the table rows.
   *
   * Only attributes that is not already applied to the profile are shown.
   *
   * @return array
   *   An array of table rows.
   */
  private function buildRows() {
    $rows = [];

    $applied_plugins = $this->linkitProfile->getAttributes()->getConfiguration();
    $all_plugins = $this->manager->getDefinitions();
    uasort($all_plugins, function ($a, $b) {
      return strnatcasecmp($a['label'], $b['label']);
    });
    foreach (array_diff_key($all_plugins, $applied_plugins) as $definition) {
      /** @var \Drupal\linkit\AttributeInterface $plugin */
      $plugin = $this->manager->createInstance($definition['id']);

      $row = [
        'label' => (string) $plugin->getLabel(),
        'description' => (string) $plugin->getDescription(),
      ];

      $rows[$plugin->getPluginId()] = $row;
    }

    return $rows;
  }
Example #12
0
 /**
  * Route title callback.
  *
  * @param \Drupal\linkit\ProfileInterface $linkit_profile
  *   The profile.
  * @param string $plugin_instance_id
  *   The plugin instance id.
  *
  * @return string
  *   The title for the attribute edit form.
  */
 public function attributeTitle(ProfileInterface $linkit_profile, $plugin_instance_id) {
   /** @var \Drupal\linkit\AttributeInterface $attribute */
   $attribute = $linkit_profile->getAttribute($plugin_instance_id);
   return $this->t('Edit %label attribute', array('%label' => $attribute->getLabel()));
 }