You are here

function eck_menu_local_actions_alter in Entity Construction Kit (ECK) 8

Implements hook_menu_local_actions_alter().

File

./eck.module, line 181
Contains hook implementations.

Code

function eck_menu_local_actions_alter(&$local_actions) {
  $eck_types = EckEntityType::loadMultiple();

  /** @var \Drupal\eck\EckEntityTypeBundleInfo $eckBundleInfo */
  $eckBundleInfo = Drupal::service('eck.entity_type.bundle.info');
  $defaults = [
    'class' => 'Drupal\\Core\\Menu\\LocalActionDefault',
    'provider' => 'eck',
    'options' => [],
    'weight' => 0,
  ];

  /* @var string $name */

  /* @var \Drupal\eck\Entity\EckEntity $type */
  foreach ($eck_types as $name => $type) {
    $local_actions['eck.bundle.' . $type
      ->id() . '.add'] = $defaults + [
      'id' => 'eck.bundle.' . $type
        ->id() . '.add',
      'route_name' => 'eck.entity.' . $type
        ->id() . '_type.add',
      'title' => t('Add @label bundle', [
        '@label' => $type
          ->label(),
      ]),
      'appears_on' => [
        'eck.entity.' . $type
          ->id() . '_type.list',
      ],
    ];
    if ($eckBundleInfo
      ->entityTypeHasBundles($type
      ->id())) {
      $bundle_machine_names = $eckBundleInfo
        ->getEntityTypeBundleMachineNames($type
        ->id());
      $local_actions['eck.' . $type
        ->id() . '.add'] = $defaults + [
        'id' => 'eck.' . $type
          ->id() . '.add',
        'title' => t('Add @label', [
          '@label' => $type
            ->label(),
        ]),
        'appears_on' => [
          'eck.entity.' . $type
            ->id() . '.list',
        ],
        'route_parameters' => [
          'eck_entity_type' => $type
            ->id(),
        ],
      ];
      if (count($bundle_machine_names) === 1) {
        $local_actions['eck.' . $type
          ->id() . '.add']['route_name'] = 'eck.entity.add';
        $local_actions['eck.' . $type
          ->id() . '.add']['route_parameters']['eck_entity_bundle'] = reset($bundle_machine_names);
      }
      else {
        $local_actions['eck.' . $type
          ->id() . '.add']['route_name'] = 'eck.entity.add_page';
      }
    }
  }
}