views_add_button-8.x-1.x-dev/src/Plugin/views_add_button/ViewsAddButtonNode.php

src/Plugin/views_add_button/ViewsAddButtonNode.php
<?php

namespace Drupal\views_add_button\Plugin\views_add_button;

use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Url;
use Drupal\views_add_button\ViewsAddButtonInterface;

/**
 * Node plugin for Views Add Button.
 *
 * @ViewsAddButton(
 *   id = "views_add_button_node",
 *   label = @Translation("ViewsAddButtonNode"),
 *   target_entity = "node"
 * )
 */
class ViewsAddButtonNode extends PluginBase implements ViewsAddButtonInterface {

  /**
   * Plugin description.
   *
   * @return string
   *   A string description.
   */
  public function description() {
    return $this->t('Views Add Button URL Generator for Node entities');
  }

  /**
   * Check for access to the appropriate "add" route.
   *
   *   Entity id as a machine name.
   *
   * @param string $bundle
   *   The bundle string.
   *   Entity context string.
   *
   * @return bool
   *   Whether we have access.
   */
  public static function checkAccess(string $entity_type, string $bundle) {
    if ($bundle) {
      return \Drupal::service('access_manager')->checkNamedRoute('node.add', ['node_type' => $bundle], \Drupal::currentUser());
    }
  }

  /**
   * Generate the add button URL.
   *
   *   Entity type ID.
   *
   * @param string $bundle
   *   Bundle ID.
   * @param array $options
   *   Array of options to be passed to the Url object.
   *   Module-specific context string.
   *
   * @return \Drupal\Core\Url
   *   Url object which is used to construct the add button link
   */
  public static function generateUrl(string $entity_type, string $bundle, array $options) {
    // Create URL from the data above.
    return Url::fromRoute('node.add', ['node_type' => $bundle], $options);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc