/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // Get the custom entity ID. $custom_entity_id = $this->entity->getEntityType()->getBundleOf(); $entities = $this->queryFactory->get($custom_entity_id)->condition('type', $this->entity->id())->execute(); if (!empty($entities)) { $caption = '<p>' . $this->formatPlural(count($entities), '%label is used by 1 entity on your site. You can not remove this entity type until you have removed all of the %label entities.', '%label is used by @count entities on your site. You may not remove %label until you have removed all of the %label entities.', ['%label' => $this->entity->label()]) . '</p>'; $form['description'] = array('#markup' => $caption); return $form; } else { return parent::buildForm($form, $form_state); } }
/** * Presents the custom entity creation form. * * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_definition * The custom entity definition. * @param \Drupal\content_entity_base\Entity\EntityTypeBaseInterface $entity_bundle * The custom entity type bundle to use. * * @return array * A form array as expected by drupal_render(). */ public function addForm(ContentEntityTypeInterface $entity_definition = NULL, EntityTypeBaseInterface $entity_bundle = NULL) { // Validate the bundle. if (!$entity_bundle || !$entity_definition) { // @todo Replace it with https://www.drupal.org/node/2571521. \Drupal::logger('content_entity_base')->error($this->t('Cannot create a @entity_type entity with an invalid bundle type of "%entity_bundle_id".'), ['@entity_type' => (string) $entity_definition, '%entity_bundle_id' => (string) $entity_bundle]); return new RedirectResponse(Url::fromRoute($entity_definition . '.add_page')->toString()); } // Get the entity storage for this entity type. $entity_storage = $this->entityManager()->getStorage($entity_definition->id()); $entity = $entity_storage->create(['type' => $entity_bundle->id()]); return $this->entityFormBuilder()->getForm($entity); }
/** * Builds a standard list of entity permissions for a given type. * * @param \Drupal\content_entity_base\Entity\EntityTypeBaseInterface $type * The machine name of the entity type. * * @return array * An array of permission names and descriptions. */ protected function buildPermissions(EntityTypeBaseInterface $type) { $entity_id = $type->bundleOf(); // Get the referring entity definition. $entity_definition = $this->entityManager->getDefinition($entity_id); $type_id = $type->id(); $type_params = ['%entity_label' => $entity_definition->getLabel(), '%type_name' => $type->label()]; return ["create {$type_id} {$entity_id}" => ['title' => $this->t('%type_name: Create new %entity_label', $type_params)], "edit own {$type_id} {$entity_id}" => ['title' => $this->t('%type_name: Edit own %entity_label', $type_params)], "edit any {$type_id} {$entity_id}" => ['title' => $this->t('%type_name: Edit any %entity_label', $type_params)], "delete own {$type_id} {$entity_id}" => ['title' => $this->t('%type_name: Delete own %entity_label', $type_params)], "delete any {$type_id} {$entity_id}" => ['title' => $this->t('%type_name: Delete any %entity_label', $type_params)], "view {$type_id} {$entity_id} revisions" => ['title' => $this->t('%type_name: View %entity_label revisions', $type_params)], "revert {$type_id} {$entity_id} revisions" => ['title' => $this->t('%type_name: Revert %entity_label revisions', $type_params), 'description' => t('Role requires permission <em>view revisions</em> and <em>edit rights</em> for %entity_label in question, or <em>Administer %entity_label</em>.', $type_params)], "delete {$type_id} {$entity_id} revisions" => ['title' => $this->t('%type_name: Delete %entity_label revisions', $type_params), 'description' => $this->t('Role requires permission to <em>view revisions</em> and <em>delete rights</em> for %entity_label in question, or <em>Administer %entity_label</em>.', $type_params)]]; }