public EntityController::addPage($entity_type_id)
Displays add links for the available bundles.
Redirects to the add form if there's only one bundle available.
Parameters
string $entity_type_id: The entity type ID.
Return value
\Symfony\Component\HttpFoundation\RedirectResponse|array If there's only one available bundle, a redirect response. Otherwise, a render array with the add links for each bundle.
File
- core/lib/Drupal/Core/Entity/Controller/EntityController.php, line 115
Class
- EntityController
- Provides the add-page and title callbacks for entities.
Namespace
Drupal\Core\Entity\Controller
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | public function addPage( $entity_type_id ) { $entity_type = $this ->entityTypeManager->getDefinition( $entity_type_id ); $bundles = $this ->entityTypeBundleInfo->getBundleInfo( $entity_type_id ); $bundle_key = $entity_type ->getKey( 'bundle' ); $bundle_entity_type_id = $entity_type ->getBundleEntityType(); $build = [ '#theme' => 'entity_add_list' , '#bundles' => [], ]; if ( $bundle_entity_type_id ) { $bundle_argument = $bundle_entity_type_id ; $bundle_entity_type = $this ->entityTypeManager->getDefinition( $bundle_entity_type_id ); $bundle_entity_type_label = $bundle_entity_type ->getLowercaseLabel(); $build [ '#cache' ][ 'tags' ] = $bundle_entity_type ->getListCacheTags(); // Build the message shown when there are no bundles. $link_text = $this ->t( 'Add a new @entity_type.' , [ '@entity_type' => $bundle_entity_type_label ]); $link_route_name = 'entity.' . $bundle_entity_type ->id() . '.add_form' ; $build [ '#add_bundle_message' ] = $this ->t( 'There is no @entity_type yet. @add_link' , [ '@entity_type' => $bundle_entity_type_label , '@add_link' => Link::createFromRoute( $link_text , $link_route_name )->toString(), ]); // Filter out the bundles the user doesn't have access to. $access_control_handler = $this ->entityTypeManager->getAccessControlHandler( $entity_type_id ); foreach ( $bundles as $bundle_name => $bundle_info ) { $access = $access_control_handler ->createAccess( $bundle_name , NULL, [], TRUE); if (! $access ->isAllowed()) { unset( $bundles [ $bundle_name ]); } $this ->renderer->addCacheableDependency( $build , $access ); } // Add descriptions from the bundle entities. $bundles = $this ->loadBundleDescriptions( $bundles , $bundle_entity_type ); } else { $bundle_argument = $bundle_key ; } $form_route_name = 'entity.' . $entity_type_id . '.add_form' ; // Redirect if there's only one bundle available. if ( count ( $bundles ) == 1) { $bundle_names = array_keys ( $bundles ); $bundle_name = reset( $bundle_names ); return $this ->redirect( $form_route_name , [ $bundle_argument => $bundle_name ]); } // Prepare the #bundles array for the template. foreach ( $bundles as $bundle_name => $bundle_info ) { $build [ '#bundles' ][ $bundle_name ] = [ 'label' => $bundle_info [ 'label' ], 'description' => isset( $bundle_info [ 'description' ]) ? $bundle_info [ 'description' ] : '' , 'add_link' => Link::createFromRoute( $bundle_info [ 'label' ], $form_route_name , [ $bundle_argument => $bundle_name ]), ]; } return $build ; } |
Please login to continue.