You are here

class FreelinkingPrepopulate in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 modules/freelinking_prepopulate/src/Plugin/freelinking/FreelinkingPrepopulate.php \Drupal\freelinking_prepopulate\Plugin\freelinking\FreelinkingPrepopulate

Freelinking prepopulate plugin.

@Freelinking( id = "freelinking_prepopulate", title = @Translation("Prepopulate"), settings = { "default_node_type" = "page", "advanced" = { "title" = "0", }, "failover" = "search", } )

Example usage:

This freelinking code


This freelinking code [[create:pagetitle]]
would produce <a href="node/add/page?edit[title][0][value]=pagetitle>pagetitle</a>

Hierarchy

Expanded class hierarchy of FreelinkingPrepopulate

1 file declares its use of FreelinkingPrepopulate
FreelinkingPrepopulateTest.php in modules/freelinking_prepopulate/tests/src/Unit/Plugin/freelinking/FreelinkingPrepopulateTest.php

File

modules/freelinking_prepopulate/src/Plugin/freelinking/FreelinkingPrepopulate.php, line 40

Namespace

Drupal\freelinking_prepopulate\Plugin\freelinking
View source
class FreelinkingPrepopulate extends FreelinkingPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Initialize method.
   *
   * @param array $configuration
   *   The plugin configuration to set.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition array.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   The entity field manager service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, ModuleHandlerInterface $moduleHandler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entityTypeManager;
    $this->entityFieldManager = $entityFieldManager;
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    $configuration = parent::getConfiguration();
    return NestedArray::mergeDeep($this
      ->defaultConfiguration(), $configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $node_types = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();
    $default = reset($node_types);
    return [
      'settings' => [
        'default_node_type' => $default
          ->id(),
        'advanced' => [
          'title' => FALSE,
        ],
        'failover' => 'search',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getIndicator() {
    return '/^create(node)?$/';
  }

  /**
   * {@inheritdoc}
   */
  public function getTip() {
    return $this
      ->t('Links to a prepopulated node/add form.');
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = [];
    $config = $this
      ->getConfiguration();
    $element['failover'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('If path alias is not found'),
      '#description' => $this
        ->t('What should freelinking do when the page is not found?'),
      '#options' => [
        'error' => $this
          ->t('Insert an error message'),
      ],
      '#default_value' => $config['settings']['failover'],
    ];
    if ($this->moduleHandler
      ->moduleExists('search')) {
      $element['failover']['#options']['search'] = $this
        ->t('Add link to search content');
    }
    $node_types = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();
    $options = array_reduce($node_types, function (&$result, $node_type) {

      /** @var \Drupal\node\Entity\NodeType $node_type */
      $result[$node_type
        ->id()] = $node_type
        ->label();
      return $result;
    }, []);
    $element['default_node_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Content type'),
      '#description' => $this
        ->t('Choose the default node type to use for Node Create links.'),
      '#options' => $options,
      '#required' => TRUE,
      '#default_value' => $config['settings']['default_node_type'],
    ];

    // @todo Add advanced field options.
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function buildLink(array $target) {
    $config = $this
      ->getConfiguration();
    $title = isset($target['text']) ? $target['text'] : $target['dest'];
    if (isset($target['type']) || isset($target['bundle'])) {
      $bundle_name = isset($target['type']) ? $target['type'] : $target['bundle'];
    }
    else {
      $bundle_name = $config['settings']['default_node_type'];
    }
    $route_params = [
      'node_type' => $bundle_name,
    ];
    $options = [
      'query' => [
        // Don't allow any HTML tags for the title field.
        'edit[title][widget][0][value]' => Xss::filter($title, []),
      ],
    ];

    // Get optional query parameters for prepopulate fields.
    // @todo https://www.drupal.org/project/prepopulate/issues/2849432
    $fields = $this->entityFieldManager
      ->getFieldDefinitions('node', $bundle_name);
    $blacklist = [
      'type',
      'bundle',
      'text',
      'dest',
    ];
    foreach ($fields as $field_name => $field_definition) {
      if (!in_array($field_name, $blacklist) && array_key_exists($field_name, $target) && !$field_definition
        ->isInternal() && !$field_definition
        ->isComputed() && !$field_definition
        ->isReadOnly()) {
        $storage_definition = $field_definition
          ->getFieldStorageDefinition();
        if ($storage_definition instanceof FieldStorageDefinitionInterface) {
          $prop = $storage_definition
            ->getMainPropertyName();
          if ($storage_definition
            ->getType() === 'entity_reference') {
            $key = '[' . $prop . ']';
          }
          else {
            $key = '[0][' . $prop . ']';
          }

          // This won't work for complex field widgets in contributed modules
          // such as select_other lists.
          $query_name = 'edit[' . $field_name . '][widget]' . $key;

          // Use the standard XSS filter for values.
          $options['query'][$query_name] = Xss::filter($target[$field_name]);
        }
      }
    }

    // @todo Implement freelinking_prepopulate_fields_from_page()?
    // @todo Implement freelinking_prepopulate_fields_from_array()?
    // Allow a module to alter query string.
    $this->moduleHandler
      ->alter('freelinking_prepopulate_query', $options['query'], $target);
    $url = Url::fromRoute('node.add', $route_params, $options);
    if ($url
      ->access()) {
      $link = [
        '#type' => 'link',
        '#title' => $title,
        '#url' => $url,
        '#attributes' => [
          'title' => $this
            ->getTip(),
        ],
      ];
    }
    elseif ($config['settings']['failover'] === 'search') {
      $link = [
        '#type' => 'link',
        '#title' => $title,
        '#url' => Url::fromUserInput('/search', [
          'query' => [
            'keys' => $title,
          ],
          'language' => $target['language'],
        ]),
      ];
    }
    else {
      $link = [
        '#theme' => 'freelink_error',
        '#plugin' => 'freelinking_prepopulate',
        '#message' => $this
          ->t('Access denied to create missing content.'),
      ];
    }
    return $link;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'), $container
      ->get('module_handler'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FreelinkingPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
FreelinkingPluginBase::getFailoverPluginId public function Get the failover plugin ID (if applicable). Overrides FreelinkingPluginInterface::getFailoverPluginId 1
FreelinkingPluginBase::isHidden public function Determine if the plugin is built-in (always on). Overrides FreelinkingPluginInterface::isHidden
FreelinkingPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FreelinkingPrepopulate::$entityFieldManager protected property The entity field manager.
FreelinkingPrepopulate::$entityTypeManager protected property The entity type manager.
FreelinkingPrepopulate::$moduleHandler protected property Module handler.
FreelinkingPrepopulate::buildLink public function Build a link with the plugin. Overrides FreelinkingPluginInterface::buildLink
FreelinkingPrepopulate::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FreelinkingPrepopulate::defaultConfiguration public function Gets default configuration for this plugin. Overrides FreelinkingPluginBase::defaultConfiguration
FreelinkingPrepopulate::getConfiguration public function Gets this plugin's configuration. Overrides FreelinkingPluginBase::getConfiguration
FreelinkingPrepopulate::getIndicator public function A regular expression string to indicate what to replace for this plugin. Overrides FreelinkingPluginInterface::getIndicator
FreelinkingPrepopulate::getTip public function Provides tips for this freelinking plugin. Overrides FreelinkingPluginInterface::getTip
FreelinkingPrepopulate::settingsForm public function Plugin configuration form. Overrides FreelinkingPluginBase::settingsForm
FreelinkingPrepopulate::__construct public function Initialize method. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.