Hi!
So I created my own module and directly beneath my mymodule-folder are the files mymodule.links.action.yml and mymodule.routing.yml.
The problem I have is, that the actions-links are not shown in the browser.
mymodule.links.action.yml
mymodule.country_add:
route_name: mymodule.country_add
title: 'Add Country'
appears_on:
- entity.mymodule_country.collection
- entity.mymodule_country.canonical
mymodule.routing.yml
entity.mymodule_country.canonical:
path: '/mymodule_country/{mymodule_country}'
defaults:
# Calls the view controller, defined in the annotation of the country entity
_entity_view: 'mymodule_country'
_title: 'Country Content'
requirements:
# Calls the access controller of the entity, $operation 'view'
_entity_access: 'mymodule_country.view'
entity.mymodule_country.collection:
path: '/mymodule_country/list'
defaults:
# Calls the list controller, defined in the annotation of the country entity.
_entity_list: 'mymodule_country'
_title: 'Country List'
requirements:
# Checks for permission directly.
_permission: 'view country entity'
mymodule.country_add:
path: '/mymodule_country/add'
defaults:
# Calls the form.add controller, defined in the country entity.
_entity_form: mymodule_country.add
_title: 'Add Country'
requirements:
_entity_create_access: 'mymodule_country'
entity.mymodule_country.edit_form:
path: '/mymodule_country/{mymodule_country}/edit'
defaults:
# Calls the form.edit controller, defined in the country entity.
_entity_form: mymodule_country.edit
_title: 'Edit Country'
requirements:
_entity_access: 'mymodule_country.edit'
entity.mymodule_country.delete_form:
path: '/mymodule_continent/{mymodule_country}/delete'
defaults:
# Calls the form.delete controller, defined in the country entity.
_entity_form: mymodule_country.delete
_title: 'Delete Country'
requirements:
_entity_access: 'mymodule_country.delete'
These look fine for me..
One of the pages is the CountryListBuilder-Page:
<?php
namespace DrupalmymoduleEntityController;
use DrupalCoreEntityEntityInterface;
use DrupalCoreEntityEntityTypeInterface;
use DrupalCoreEntityEntityListBuilder;
use DrupalCoreEntityEntityStorageInterface;
use DrupalCoreRoutingUrlGeneratorInterface;
use DrupalCoreUrl;
use SymfonyComponentDependencyInjectionContainerInterface;
/**
* Provides a list controller for mymodule entity.
*
* @ingroup mymodule
*/
class CountryListBuilder extends EntityListBuilder {
/**
* The url generator.
*
* @var DrupalCoreRoutingUrlGeneratorInterface
*/
protected $urlGenerator;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type, $container->get('entity.manager')->getStorage($entity_type->id()), $container->get('url_generator')
);
}
/**
* Constructs a new CountryListBuilder object.
*
* @param DrupalCoreEntityEntityTypeInterface $entity_type
* The entity type definition.
* @param DrupalCoreEntityEntityStorageInterface $storage
* The entity storage class.
* @param DrupalCoreRoutingUrlGeneratorInterface $url_generator
* The url generator.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, UrlGeneratorInterface $url_generator) {
parent::__construct($entity_type, $storage);
$this->urlGenerator = $url_generator;
}
/**
* {@inheritdoc}
*
* We override ::render() so that we can add our own content above the table.
* parent::render() is where EntityListBuilder creates the table using our
* buildHeader() and buildRow() implementations.
*/
public function render() {
$build['table'] = parent::render();
return $build;
}
/**
* {@inheritdoc}
*
* Building the header and content lines for the country list.
*
* Calling the parent::buildHeader() adds a column for the possible actions
* and inserts the 'edit' and 'delete' links as defined for the entity type.
*/
public function buildHeader() {
$header['country_id'] = $this->t('CountryID');
$header['name'] = $this->t('Name');
$header['continent_id'] = $this->t('Continent');
$header['active'] = $this->t('Active');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var $entity DrupalmymoduleEntityCountry */
$ents = $entity->referencedEntities();
$row['country_id'] = $entity->id();
$row['name'] = $entity->link();
foreach($ents as $ent){
switch($ent->getEntityTypeId()){
case "mymodule_continent":
$row['continent_id'] = Drupal::l(
$ent->label(),
Url::fromRoute('entity.mymodule_continent.canonical', array(
'mymodule_continent' => $ent->id(),
)
)
);
break;
}
}
$row['active'] = $entity->active->value;
return $row + parent::buildRow($entity);
}
}
Do I have to set/define anything anywhere to the mymodule.links.action.yml to work?
Or is there a better way to do this at all?
Drupal version:
Source: https://www.drupal.org/taxonomy/term/4/feed