You are here

class FarmActions in farmOS 2.x

Defines farmOS action links.

Hierarchy

Expanded class hierarchy of FarmActions

1 string reference to 'FarmActions'
farm_ui_action.links.action.yml in modules/core/ui/action/farm_ui_action.links.action.yml
modules/core/ui/action/farm_ui_action.links.action.yml

File

modules/core/ui/action/src/Plugin/Derivative/FarmActions.php, line 11

Namespace

Drupal\farm_ui_action\Plugin\Derivative
View source
class FarmActions extends DeriverBase {

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {

    // Load available entity types.
    $entity_types = array_keys(\Drupal::entityTypeManager()
      ->getDefinitions());

    // Define the farmOS entity types we care about.
    $farm_types = [
      'asset',
      'log',
      'plan',
    ];

    // Iterate through the farmOS entity types.
    foreach ($farm_types as $type) {

      // If the entity type does not exist, skip it.
      if (!in_array($type, $entity_types)) {
        continue;
      }

      // Generate a link to [entity-type]/add.
      $name = 'farm.add.' . $type;
      $this->derivatives[$name] = $base_plugin_definition;
      $this->derivatives[$name]['title'] = 'Add ' . Unicode::ucfirst($type);
      $this->derivatives[$name]['route_name'] = 'entity.' . $type . '.add_page';

      // Add it to entity Views, if the farm_ui_views module is enabled.
      if (\Drupal::moduleHandler()
        ->moduleExists('farm_ui_views')) {
        $this->derivatives[$name]['appears_on'][] = 'view.farm_' . $type . '.page';

        // If this is a log, also add it to view.farm_log.page_user.
        if ($type == 'log') {
          $this->derivatives[$name]['appears_on'][] = 'view.farm_log.page_user';
        }
      }

      // Add it to farm.dashboard, if the farm_ui_dashboard module is enabled.
      if (\Drupal::moduleHandler()
        ->moduleExists('farm_ui_dashboard')) {
        $this->derivatives[$name]['appears_on'][] = 'farm.dashboard';
      }

      // Generate a link to [entity-type]/add/[bundle].
      $name = 'farm.add.' . $type . '.bundle';
      $this->derivatives[$name] = $base_plugin_definition;
      $this->derivatives[$name]['route_name'] = 'entity.' . $type . '.add_form';
      $this->derivatives[$name]['class'] = 'Drupal\\farm_ui_action\\Plugin\\Menu\\LocalAction\\AddEntity';
      $this->derivatives[$name]['entity_type'] = $type;

      // Add the entity_bundles cache tag so action links are recreated after
      // new bundles are installed.
      $this->derivatives[$name]['cache_tags'] = [
        'entity_bundles',
      ];

      // Add it to entity bundle Views, if the farm_ui_views module is enabled.
      if (\Drupal::moduleHandler()
        ->moduleExists('farm_ui_views')) {
        $this->derivatives[$name]['appears_on'][] = 'view.farm_' . $type . '.page_type';
        $this->derivatives[$name]['bundle_parameter'] = 'arg_0';
      }

      // Generate links to [entity-type]/add/[bundle]?asset=[id] on asset pages.
      if ($type == 'log') {
        $bundles = \Drupal::service('entity_type.bundle.info')
          ->getBundleInfo('log');
        foreach ($bundles as $bundle => $bundle_info) {
          $name = 'farm.asset.add.' . $type . '.' . $bundle;
          $this->derivatives[$name] = $base_plugin_definition;
          $this->derivatives[$name]['route_name'] = 'entity.' . $type . '.add_form';
          $this->derivatives[$name]['class'] = 'Drupal\\farm_ui_action\\Plugin\\Menu\\LocalAction\\AddEntity';
          $this->derivatives[$name]['entity_type'] = $type;
          $this->derivatives[$name]['bundle'] = $bundle;
          $this->derivatives[$name]['appears_on'][] = 'entity.asset.canonical';
          $this->derivatives[$name]['prepopulate'] = [
            'asset' => [
              'route_parameter' => 'asset',
            ],
          ];
          $this->derivatives[$name]['cache_tags'] = [
            'entity_bundles',
          ];
        }
      }
    }
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
FarmActions::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions