/**
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info
  *
  * @return mixed
  */
 protected function permissionLabels(EntityTypeInterface $entity_info)
 {
     $labels = $entity_info->get("permission_labels");
     if (!isset($labels['singular'])) {
         $labels['singular'] = $entity_info->getLabel();
     }
     if (!isset($labels['plural'])) {
         $labels['plural'] = $entity_info->getLabel();
     }
     return $labels;
 }
Example #2
0
 /**
  * Overrides \Drupal\views\Plugin\views\filter\InOperator::getValueOptions().
  */
 public function getValueOptions()
 {
     if (!isset($this->value_options)) {
         $types = entity_get_bundles($this->entityTypeId);
         $this->value_title = t('@entity types', array('@entity' => $this->entityType->getLabel()));
         $options = array();
         foreach ($types as $type => $info) {
             $options[$type] = $info['label'];
         }
         asort($options);
         $this->value_options = $options;
     }
     return $this->value_options;
 }
 /**
  * Tests the getBaseFieldDefinitions() method with an invalid definition.
  *
  * @covers ::getBaseFieldDefinitions
  * @covers ::buildBaseFieldDefinitions
  *
  * @expectedException \LogicException
  */
 public function testGetBaseFieldDefinitionsInvalidDefinition()
 {
     $this->setUpEntityWithFieldDefinition(FALSE, 'langcode', array('langcode' => 'langcode'));
     $this->entityType->isTranslatable()->willReturn(TRUE);
     $this->entityType->getLabel()->willReturn('the_label');
     $this->entityManager->getBaseFieldDefinitions('test_entity_type');
 }
 /**
  * {@inheritdoc}
  */
 public function getViewsData()
 {
     $data = [];
     // @todo In theory we should use the data table as base table, as this would
     //   save one pointless join (and one more for every relationship).
     // @see https://drupal.org/node/2337509
     $base_table = $this->entityType->getBaseTable();
     $base_field = $this->entityType->getKey('id');
     $data_table = $this->entityType->getDataTable();
     $revision_table = $this->entityType->getRevisionTable();
     $revision_data_table = $this->entityType->getRevisionDataTable();
     $revision_field = $this->entityType->getKey('revision');
     // Setup base information of the views data.
     $data[$base_table]['table']['entity type'] = $this->entityType->id();
     $data[$base_table]['table']['group'] = $this->entityType->getLabel();
     $data[$base_table]['table']['base'] = ['field' => $base_field, 'title' => $this->entityType->getLabel()];
     if ($label_key = $this->entityType->getKey('label')) {
         if ($data_table) {
             $data[$base_table]['table']['base']['defaults'] = array('field' => $label_key, 'table' => $data_table);
         } else {
             $data[$base_table]['table']['base']['defaults'] = array('field' => $label_key);
         }
     }
     // Setup relations to the revisions/property data.
     if ($data_table) {
         $data[$data_table]['table']['join'][$base_table] = ['left_field' => $base_field, 'field' => $base_field, 'type' => 'INNER'];
         $data[$data_table]['table']['entity type'] = $this->entityType->id();
         $data[$data_table]['table']['group'] = $this->entityType->getLabel();
     }
     if ($revision_table) {
         $data[$revision_table]['table']['entity type'] = $this->entityType->id();
         $data[$revision_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]);
         $data[$revision_table]['table']['base'] = array('field' => $revision_field, 'title' => $this->t('@entity_type revisions', array('@entity_type' => $this->entityType->getLabel())));
         // Join the revision table to the base table.
         $data[$revision_table]['table']['join'][$base_table] = array('left_field' => $revision_field, 'field' => $revision_field, 'type' => 'INNER');
         if ($revision_data_table) {
             $data[$revision_data_table]['table']['entity type'] = $this->entityType->id();
             $data[$revision_data_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]);
             $data[$revision_data_table]['table']['join'][$revision_table] = array('left_field' => $revision_field, 'field' => $revision_field, 'type' => 'INNER');
         }
     }
     // Load all typed data definitions of all fields. This should cover each of
     // the entity base, revision, data tables.
     $field_definitions = $this->entityManager->getBaseFieldDefinitions($this->entityType->id());
     if ($table_mapping = $this->storage->getTableMapping()) {
         // Iterate over each table we have so far and collect field data for each.
         // Based on whether the field is in the field_definitions provided by the
         // entity manager.
         // @todo We should better just rely on information coming from the entity
         //   storage.
         // @todo https://drupal.org/node/2337511
         foreach ($table_mapping->getTableNames() as $table) {
             foreach ($table_mapping->getFieldNames($table) as $field_name) {
                 $this->mapFieldDefinition($table, $field_name, $field_definitions[$field_name], $table_mapping, $data[$table]);
             }
         }
     }
     return $data;
 }
 protected function collectionRoute(EntityTypeInterface $entity_type)
 {
     $route = new Route($entity_type->getLinkTemplate('collection'));
     $route->setDefault('_title', $entity_type->getLabel() . ' content');
     $route->setDefault('_entity_list', $entity_type->id());
     $route->setRequirement('_permission', 'view ' . $entity_type->id() . ' entity');
     return $route;
 }
 /**
  * {@inheritdoc}
  *
  * Builds the entity listing as renderable array for theme_table().
  *
  * @todo Add a link to add a new item to the #empty text.
  */
 public function render()
 {
     $build = array('#type' => 'table', '#header' => $this->buildHeader(), '#title' => $this->getTitle(), '#rows' => array(), '#empty' => $this->t('There is no @label yet.', array('@label' => $this->entityType->getLabel())));
     foreach ($this->load() as $entity) {
         if ($row = $this->buildRow($entity)) {
             $build['#rows'][$entity->id()] = $row;
         }
     }
     return $build;
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function summary()
 {
     if (count($this->configuration['bundles']) > 1) {
         $bundles = $this->configuration['bundles'];
         $last = array_pop($bundles);
         $bundles = implode(', ', $bundles);
         return $this->t('The @entity_type @bundle_type is @bundles or @last', array('@entity_type' => strtolower($this->bundleOf->getLabel()), '@bundle_type' => strtolower($this->bundleType->getLabel()), '@bundles' => $bundles, '@last' => $last));
     }
     $bundle = reset($this->configuration['bundles']);
     return $this->t('The @entity_type @bundle_type is @bundle', array('@entity_type' => strtolower($this->bundleOf->getLabel()), '@bundle_type' => strtolower($this->bundleType->getLabel()), '@bundle' => $bundle));
 }
Example #8
0
 /**
  * Provides the bundle label with a fallback when not defined.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  *   The entity type we are looking the bundle label for.
  *
  * @return \Drupal\Core\StringTranslation\TranslatableMarkup
  *   The entity bundle label or a fallback label.
  */
 protected function getEntityBundleLabel($entity_type)
 {
     if ($label = $entity_type->getBundleLabel()) {
         return $this->t('@label', ['@label' => $label]);
     }
     $fallback = $entity_type->getLabel();
     if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
         // This is a better fallback.
         $fallback = $this->entityManager->getDefinition($bundle_entity_type)->getLabel();
     }
     return $this->t('@label bundle', ['@label' => $fallback]);
 }
 /**
  * Sets the entity links in case corresponding link templates exist.
  *
  * @param array $data
  *   The views data of the base table.
  */
 protected function addEntityLinks(array &$data)
 {
     $entity_type_id = $this->entityType->id();
     $t_arguments = ['@entity_type_label' => $this->entityType->getLabel()];
     if ($this->entityType->hasLinkTemplate('canonical')) {
         $data['view_' . $entity_type_id] = ['field' => ['title' => $this->t('Link to @entity_type_label', $t_arguments), 'help' => $this->t('Provide a view link to the @entity_type_label.', $t_arguments), 'id' => 'entity_link']];
     }
     if ($this->entityType->hasLinkTemplate('edit-form')) {
         $data['edit_' . $entity_type_id] = ['field' => ['title' => $this->t('Link to edit @entity_type_label', $t_arguments), 'help' => $this->t('Provide an edit link to the @entity_type_label.', $t_arguments), 'id' => 'entity_link_edit']];
     }
     if ($this->entityType->hasLinkTemplate('delete-form')) {
         $data['delete_' . $entity_type_id] = ['field' => ['title' => $this->t('Link to delete @entity_type_label', $t_arguments), 'help' => $this->t('Provide a delete link to the @entity_type_label.', $t_arguments), 'id' => 'entity_link_delete']];
     }
 }
 /**
  * {@inheritdoc}
  *
  * Builds the entity listing as renderable array for table.html.twig.
  *
  * @todo Add a link to add a new item to the #empty text.
  */
 public function render()
 {
     $build['table'] = array('#type' => 'table', '#header' => $this->buildHeader(), '#title' => $this->getTitle(), '#rows' => array(), '#empty' => $this->t('There is no @label yet.', array('@label' => $this->entityType->getLabel())), '#cache' => ['contexts' => $this->entityType->getListCacheContexts()]);
     foreach ($this->load() as $entity) {
         if ($row = $this->buildRow($entity)) {
             $build['table']['#rows'][$entity->id()] = $row;
         }
     }
     // Only add the pager if a limit is specified.
     if ($this->limit) {
         $build['pager'] = array('#type' => 'pager');
     }
     return $build;
 }
 /**
  * Gets the add-form route.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  *   The entity type.
  *
  * @return \Symfony\Component\Routing\Route|null
  *   The generated route, if available.
  */
 protected function getAddFormRoute(EntityTypeInterface $entity_type)
 {
     if ($entity_type->hasLinkTemplate('add-form')) {
         $entity_type_id = $entity_type->id();
         $route = new Route($entity_type->getLinkTemplate('add-form'));
         // Use the add form handler, if available, otherwise default.
         $operation = 'default';
         if ($entity_type->getFormClass('add')) {
             $operation = 'add';
         }
         $route->setDefaults(['_entity_form' => "{$entity_type_id}.{$operation}", '_title' => "Add {$entity_type->getLabel()}"])->setRequirement('_entity_create_access', $entity_type_id)->setOption('parameters', [$entity_type_id => ['type' => 'entity:' . $entity_type_id]])->setOption('_admin_route', TRUE);
         return $route;
     }
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
 {
     $fields['id'] = BaseFieldDefinition::create('integer')->setLabel(t('Custom entity ID'))->setDescription(t('The custom entity ID.'))->setReadOnly(TRUE)->setSetting('unsigned', TRUE);
     $fields['uuid'] = BaseFieldDefinition::create('uuid')->setLabel(t('UUID'))->setDescription(t('The custom entity UUID.'))->setReadOnly(TRUE);
     $fields['revision_id'] = BaseFieldDefinition::create('integer')->setLabel(t('Revision ID'))->setDescription(t('The revision ID.'))->setReadOnly(TRUE)->setSetting('unsigned', TRUE);
     $fields['langcode'] = BaseFieldDefinition::create('language')->setLabel(t('Language'))->setDescription(t('The custom entity language code.'))->setTranslatable(TRUE)->setRevisionable(TRUE)->setDisplayOptions('view', array('type' => 'hidden'))->setDisplayOptions('form', array('type' => 'language_select', 'weight' => 2));
     $fields['name'] = BaseFieldDefinition::create('string')->setLabel(t('Administrative Title'))->setDescription(t('A brief description of this @entity_label entry.', ['@entity_label' => $entity_type->getLabel()]))->setRevisionable(TRUE)->setTranslatable(TRUE)->setRequired(TRUE)->setDisplayOptions('form', array('type' => 'string_textfield', 'weight' => -5))->setDisplayConfigurable('form', TRUE);
     $fields['uid'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('Authored by'))->setDescription(t('The username of the entity author.'))->setRevisionable(TRUE)->setSetting('target_type', 'user')->setDefaultValueCallback('Drupal\\content_entity_base\\Entity\\EntityBase::getCurrentUserId')->setTranslatable(TRUE)->setDisplayOptions('view', array('label' => 'hidden', 'type' => 'author', 'weight' => 0))->setDisplayOptions('form', array('type' => 'entity_reference_autocomplete', 'weight' => 5, 'settings' => array('match_operator' => 'CONTAINS', 'size' => '60', 'placeholder' => '')))->setDisplayConfigurable('form', TRUE);
     $fields['status'] = BaseFieldDefinition::create('boolean')->setLabel(t('Publishing status'))->setDescription(t('A boolean indicating whether the node is published.'))->setRevisionable(TRUE)->setTranslatable(TRUE)->setDefaultValue(TRUE);
     $fields['type'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('Entity type (Bundle)'))->setDescription(t('The entity type.'))->setSetting('target_type', $entity_type->getBundleEntityType());
     $fields['revision_log'] = BaseFieldDefinition::create('string_long')->setLabel(t('Revision log message'))->setDescription(t('The log entry explaining the changes in this revision.'))->setRevisionable(TRUE);
     $fields['created'] = BaseFieldDefinition::create('created')->setLabel(t('Created'))->setDescription(t('The time that the entity was created.'))->setTranslatable(TRUE)->setRevisionable(TRUE);
     $fields['changed'] = BaseFieldDefinition::create('changed')->setLabel(t('Changed'))->setDescription(t('The time that the custom entity was last edited.'))->setTranslatable(TRUE)->setRevisionable(TRUE);
     $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')->setLabel(t('Revision translation affected'))->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))->setReadOnly(TRUE)->setRevisionable(TRUE)->setTranslatable(TRUE);
     return $fields;
 }
Example #13
0
  /**
   * Gets the entity_clone route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getEntityCloneRoute(EntityTypeInterface $entity_type) {
    if ($clone_form = $entity_type->getLinkTemplate('clone-form')) {
      $entity_type_id = $entity_type->id();
      $route = new Route($clone_form);
      $route
        ->addDefaults([
          '_form' => '\Drupal\entity_clone\Form\EntityCloneForm',
          '_title' => 'Clone ' . $entity_type->getLabel(),
        ])
        ->addRequirements([
          '_permission' => 'clone ' . $entity_type->id() . ' entity',
        ])
        ->setOption('_entity_clone_entity_type_id', $entity_type_id)
        ->setOption('_admin_route', TRUE)
        ->setOption('parameters', [
          $entity_type_id => ['type' => 'entity:' . $entity_type_id],
        ]);

      return $route;
    }
  }
 /**
  * Gets the settings form route.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  *   The entity type.
  *
  * @return \Symfony\Component\Routing\Route|null
  *   The generated route, if available.
  */
 protected function getSettingsFormRoute(EntityTypeInterface $entity_type)
 {
     if (!$entity_type->getBundleEntityType()) {
         $route = new Route("/admin/structure/{$entity_type->id()}/settings");
         $route->setDefaults(['_form' => 'Drupal\\drupalbristol_sponsors\\Form\\SponsorEntitySettingsForm', '_title' => "{$entity_type->getLabel()} settings"])->setRequirement('_permission', $entity_type->getAdminPermission())->setOption('_admin_route', TRUE);
         return $route;
     }
 }
Example #15
0
 /**
  * {@inheritdoc}
  */
 public function getViewsData()
 {
     $data = [];
     $base_table = $this->entityType->getBaseTable();
     $base_field = $this->entityType->getKey('id');
     $data_table = $this->entityType->getDataTable();
     $revision_table = $this->entityType->getRevisionTable();
     $revision_data_table = $this->entityType->getRevisionDataTable();
     $revision_field = $this->entityType->getKey('revision');
     // Setup base information of the views data.
     $data[$base_table]['table']['group'] = $this->entityType->getLabel();
     $data[$base_table]['table']['provider'] = $this->entityType->getProvider();
     $views_base_table = $base_table;
     if ($data_table) {
         $views_base_table = $data_table;
     }
     $data[$views_base_table]['table']['base'] = ['field' => $base_field, 'title' => $this->entityType->getLabel(), 'cache_contexts' => $this->entityType->getListCacheContexts()];
     $data[$base_table]['table']['entity revision'] = FALSE;
     if ($label_key = $this->entityType->getKey('label')) {
         if ($data_table) {
             $data[$views_base_table]['table']['base']['defaults'] = array('field' => $label_key, 'table' => $data_table);
         } else {
             $data[$views_base_table]['table']['base']['defaults'] = array('field' => $label_key);
         }
     }
     $data[$base_table]['operations'] = array('field' => array('title' => $this->t('Operations links'), 'help' => $this->t('Provides links to perform entity operations.'), 'id' => 'entity_operations'));
     // Setup relations to the revisions/property data.
     if ($data_table) {
         $data[$base_table]['table']['join'][$data_table] = ['left_field' => $base_field, 'field' => $base_field, 'type' => 'INNER'];
         $data[$data_table]['table']['group'] = $this->entityType->getLabel();
         $data[$data_table]['table']['provider'] = $this->entityType->getProvider();
         $data[$data_table]['table']['entity revision'] = FALSE;
     }
     if ($revision_table) {
         $data[$revision_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]);
         $data[$revision_table]['table']['provider'] = $this->entityType->getProvider();
         $views_revision_base_table = $revision_table;
         if ($revision_data_table) {
             $views_revision_base_table = $revision_data_table;
         }
         $data[$views_revision_base_table]['table']['entity revision'] = TRUE;
         $data[$views_revision_base_table]['table']['base'] = array('field' => $revision_field, 'title' => $this->t('@entity_type revisions', array('@entity_type' => $this->entityType->getLabel())));
         // Join the revision table to the base table.
         $data[$views_revision_base_table]['table']['join'][$views_base_table] = array('left_field' => $revision_field, 'field' => $revision_field, 'type' => 'INNER');
         if ($revision_data_table) {
             $data[$revision_data_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]);
             $data[$revision_data_table]['table']['entity revision'] = TRUE;
             $data[$revision_data_table]['table']['join'][$revision_table] = array('left_field' => $revision_field, 'field' => $revision_field, 'type' => 'INNER');
         }
     }
     // Load all typed data definitions of all fields. This should cover each of
     // the entity base, revision, data tables.
     $field_definitions = $this->entityManager->getBaseFieldDefinitions($this->entityType->id());
     if ($table_mapping = $this->storage->getTableMapping()) {
         // Iterate over each table we have so far and collect field data for each.
         // Based on whether the field is in the field_definitions provided by the
         // entity manager.
         // @todo We should better just rely on information coming from the entity
         //   storage.
         // @todo https://drupal.org/node/2337511
         foreach ($table_mapping->getTableNames() as $table) {
             foreach ($table_mapping->getFieldNames($table) as $field_name) {
                 $this->mapFieldDefinition($table, $field_name, $field_definitions[$field_name], $table_mapping, $data[$table]);
             }
         }
     }
     // Add the entity type key to each table generated.
     $entity_type_id = $this->entityType->id();
     array_walk($data, function (&$table_data) use($entity_type_id) {
         $table_data['table']['entity type'] = $entity_type_id;
     });
     return $data;
 }
 /**
  * Returns a list of permissions per entity type and graph.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $type
  *   The entity type.
  * @param string $graph
  *   The machine name for the graph.
  *
  * @return array
  *   An associative array of permission names and descriptions.
  */
 protected function buildPermissions(EntityTypeInterface $type, $graph)
 {
     $type_id = $type->id();
     $type_params = array('%type_name' => $type->getLabel(), '%graph_name' => $graph);
     return array("view {$type_id} {$graph} graph" => array('title' => $this->t('%type_name: View %graph_name graph', $type_params)));
 }
Example #17
0
 /**
  * Returns the label of the entity being processed.
  *
  * @return string
  *   The label of the entity.
  */
 protected function entityLabel()
 {
     return $this->entityInfo->getLabel();
 }
 /**
  * Set all derivatives for an entity type.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  * @param array $base_plugin_definition
  */
 protected function setConfigurableFieldsDerivatives(EntityTypeInterface $entity_type, array $base_plugin_definition)
 {
     /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */
     $field_storages = $this->field_manager->getFieldStorageDefinitions($entity_type->id());
     foreach ($field_storages as $field_id => $field_storage) {
         if ($field_storage->getType() == 'datetime') {
             $entity_type_id = $entity_type->id();
             // Find better way to get table name.
             $field_table = $entity_type_id . '__' . $field_id;
             $field_table_data = $this->viewsData->get($field_table);
             if (isset($field_table_data[$field_id])) {
                 $derivative = [];
                 $field_info = $field_table_data[$field_id];
                 $derivative['field_id'] = $field_id;
                 $join_tables = array_keys($field_table_data['table']['join']);
                 // @todo Will there ever be more than 1 tables here?
                 $join_table = array_pop($join_tables);
                 $join_table_data = $this->viewsData->get($join_table);
                 $derivative = ['replacements' => ['field_id' => $field_id, 'entity_type' => $entity_type_id, 'entity_label' => $entity_type->getLabel(), 'field_label' => $field_info['title'], 'base_table' => $join_table, 'field_table' => $field_table, 'default_field_id' => $this->getTableDefaultField($join_table_data, $entity_type_id), 'base_field' => $this->getTableBaseField($join_table_data)], 'view_template_id' => 'calendar_config_field'];
                 $this->setDerivative($derivative, $base_plugin_definition);
                 //$this->setDerivative($field_info, $entity_type, $field_table_data, $base_plugin_definition);
             }
         }
     }
 }
 /**
  * Gets the collection route.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  *   The entity type.
  *
  * @return \Symfony\Component\Routing\Route|null
  *   The generated route, if available.
  */
 protected function getCollectionRoute(EntityTypeInterface $entity_type)
 {
     // If the entity type does not provide an admin permission, there is no way
     // to control access, so we cannot provide a route in a sensible way.
     if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass() && ($admin_permission = $entity_type->getAdminPermission())) {
         $route = new Route($entity_type->getLinkTemplate('collection'));
         $route->addDefaults(['_entity_list' => $entity_type->id(), '_title' => '@label entities', '_title_arguments' => ['@label' => $entity_type->getLabel()]])->setRequirement('_permission', $admin_permission);
         return $route;
     }
 }
Example #20
0
 /**
  * Returns the label of the entity type being processed.
  *
  * @return string
  *   The label of the entity type.
  */
 protected function entityTypeLabel()
 {
     return $this->entityType->getLabel();
 }
 /**
  * Returns the bundle label for a given entity type.
  *
  * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  *   The entity type.
  *
  * @return string
  *   The bundle label.
  */
 protected function getBundleLabel(EntityTypeInterface $entity_type)
 {
     if ($entity_type->getBundleLabel()) {
         return $entity_type->getBundleLabel();
     }
     if ($entity_type->getBundleEntityType()) {
         return \Drupal::entityTypeManager()->getDefinition($entity_type->getBundleEntityType())->getLabel();
     }
     return $this->t('@label type', ['@label' => $entity_type->getLabel()]);
 }