class FreelinkingPrepopulate in Freelinking 8.3
Same name and namespace in other branches
- 4.0.x 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait- class \Drupal\freelinking\Plugin\FreelinkingPluginBase implements FreelinkingPluginInterface- class \Drupal\freelinking_prepopulate\Plugin\freelinking\FreelinkingPrepopulate implements ContainerFactoryPluginInterface
 
 
- class \Drupal\freelinking\Plugin\FreelinkingPluginBase implements FreelinkingPluginInterface
 
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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 39 
Namespace
Drupal\freelinking_prepopulate\Plugin\freelinkingView 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) {
          $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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| DependencySerializationTrait:: | protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| FreelinkingPluginBase:: | public | function | Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: | |
| FreelinkingPluginBase:: | public | function | Get the failover plugin ID (if applicable). Overrides FreelinkingPluginInterface:: | 1 | 
| FreelinkingPluginBase:: | public | function | Determine if the plugin is built-in (always on). Overrides FreelinkingPluginInterface:: | |
| FreelinkingPluginBase:: | public | function | Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: | |
| FreelinkingPrepopulate:: | protected | property | The entity field manager. | |
| FreelinkingPrepopulate:: | protected | property | The entity type manager. | |
| FreelinkingPrepopulate:: | protected | property | Module handler. | |
| FreelinkingPrepopulate:: | public | function | Build a link with the plugin. Overrides FreelinkingPluginInterface:: | |
| FreelinkingPrepopulate:: | public static | function | Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: | |
| FreelinkingPrepopulate:: | public | function | Gets default configuration for this plugin. Overrides FreelinkingPluginBase:: | |
| FreelinkingPrepopulate:: | public | function | Gets this plugin's configuration. Overrides FreelinkingPluginBase:: | |
| FreelinkingPrepopulate:: | public | function | A regular expression string to indicate what to replace for this plugin. Overrides FreelinkingPluginInterface:: | |
| FreelinkingPrepopulate:: | public | function | Provides tips for this freelinking plugin. Overrides FreelinkingPluginInterface:: | |
| FreelinkingPrepopulate:: | public | function | Plugin configuration form. Overrides FreelinkingPluginBase:: | |
| FreelinkingPrepopulate:: | public | function | Initialize method. Overrides PluginBase:: | |
| MessengerTrait:: | protected | property | The messenger. | 29 | 
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| PluginBase:: | protected | property | Configuration information passed into the plugin. | 1 | 
| PluginBase:: | protected | property | The plugin implementation definition. | 1 | 
| PluginBase:: | protected | property | The plugin_id. | |
| PluginBase:: | constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
| PluginBase:: | public | function | Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: | 3 | 
| PluginBase:: | public | function | Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: | |
| PluginBase:: | public | function | Determines if the plugin is configurable. | |
| StringTranslationTrait:: | protected | property | The string translation service. | 1 | 
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | 
