本文整理汇总了PHP中Drupal\Core\Entity\EntityTypeInterface::getKey方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityTypeInterface::getKey方法的具体用法?PHP EntityTypeInterface::getKey怎么用?PHP EntityTypeInterface::getKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\EntityTypeInterface
的用法示例。
在下文中一共展示了EntityTypeInterface::getKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: init
/**
* Overrides \Drupal\views\Plugin\views\filter\InOperator::init().
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL)
{
parent::init($view, $display, $options);
$this->entityTypeId = $this->getEntityType();
$this->entityType = \Drupal::entityManager()->getDefinition($this->entityTypeId);
$this->real_field = $this->entityType->getKey('bundle');
}
示例2: init
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL)
{
parent::init($view, $display, $options);
$this->entityTypeId = $this->definition['entity_type'];
$this->entityType = $this->entityManager->getDefinition($this->entityTypeId);
$this->base_table = $this->entityType->getDataTable() ?: $this->entityType->getBaseTable();
$this->base_field = $this->entityType->getKey('id');
}
示例3: __construct
/**
* Constructs an EntityStorageBase instance.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
*/
public function __construct(EntityTypeInterface $entity_type)
{
$this->entityTypeId = $entity_type->id();
$this->entityType = $entity_type;
$this->idKey = $this->entityType->getKey('id');
$this->uuidKey = $this->entityType->getKey('uuid');
$this->entityClass = $this->entityType->getClass();
}
示例4: getEntityIds
/**
* Loads entity IDs using a pager sorted by the entity id.
*
* @return array
* An array of entity IDs.
*/
protected function getEntityIds()
{
$query = $this->getStorage()->getQuery()->sort($this->entityType->getKey('id'));
// Only add the pager if a limit is specified.
if ($this->limit) {
$query->pager($this->limit);
}
return $query->execute();
}
示例5: processViewsDataForEntityReference
/**
* Processes the views data for an entity reference field.
*
* @param string $table
* The table the language field is added to.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param array $views_field
* The views field data.
* @param string $field_column_name
* The field column being processed.
*/
protected function processViewsDataForEntityReference($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name)
{
// @todo Should the actual field handler respect that this just renders a
// number?
// @todo Create an optional entity field handler, that can render the
// entity.
// @see https://www.drupal.org/node/2322949
if ($entity_type_id = $field_definition->getItemDefinition()->getSetting('target_type')) {
$entity_type = $this->entityManager->getDefinition($entity_type_id);
if ($entity_type instanceof ContentEntityType) {
$views_field['relationship'] = ['base' => $this->getViewsTableForEntityType($entity_type), 'base field' => $entity_type->getKey('id'), 'label' => $entity_type->getLabel(), 'title' => $entity_type->getLabel(), 'id' => 'standard'];
$views_field['field']['id'] = 'field';
$views_field['argument']['id'] = 'numeric';
$views_field['filter']['id'] = 'numeric';
$views_field['sort']['id'] = 'standard';
} else {
$views_field['field']['id'] = 'field';
$views_field['argument']['id'] = 'string';
$views_field['filter']['id'] = 'string';
$views_field['sort']['id'] = 'standard';
}
}
if ($field_definition->getName() == $this->entityType->getKey('bundle')) {
$views_field['filter']['id'] = 'bundle';
}
}
示例6: defaultDisplayFiltersUser
/**
* Retrieves filter information based on user input for the default display.
*
* @param array $form
* The full wizard form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the wizard form.
*
* @return array
* An array of filter arrays keyed by ID. A sort array contains the options
* accepted by a filter handler.
*/
protected function defaultDisplayFiltersUser(array $form, FormStateInterface $form_state)
{
$filters = array();
if (($type = $form_state->getValue(array('show', 'type'))) && $type != 'all') {
$bundle_key = $this->entityType->getKey('bundle');
// Figure out the table where $bundle_key lives. It may not be the same as
// the base table for the view; the taxonomy vocabulary machine_name, for
// example, is stored in taxonomy_vocabulary, not taxonomy_term_data.
module_load_include('inc', 'views_ui', 'admin');
$fields = Views::viewsDataHelper()->fetchFields($this->base_table, 'filter');
if (isset($fields[$this->base_table . '.' . $bundle_key])) {
$table = $this->base_table;
} else {
foreach ($fields as $field_name => $value) {
if ($pos = strpos($field_name, '.' . $bundle_key)) {
$table = substr($field_name, 0, $pos);
break;
}
}
}
$table_data = Views::viewsData()->get($table);
// If the 'in' operator is being used, map the values to an array.
$handler = $table_data[$bundle_key]['filter']['id'];
$handler_definition = Views::pluginManager('filter')->getDefinition($handler);
if ($handler == 'in_operator' || is_subclass_of($handler_definition['class'], 'Drupal\\views\\Plugin\\views\\filter\\InOperator')) {
$value = array($type => $type);
} else {
$value = $type;
}
$filters[$bundle_key] = array('id' => $bundle_key, 'table' => $table, 'field' => $bundle_key, 'value' => $value);
}
return $filters;
}
示例7: buildAdvancedForm
/**
* {@inheritdoc}
*/
public function buildAdvancedForm(array $form, FormStateInterface $form_state)
{
if ($bundle_key = $this->entityType->getKey('bundle')) {
$form['values'][$bundle_key] = ['#type' => 'select', '#options' => $this->bundleOptions(), '#title' => $this->bundleLabel(), '#required' => TRUE, '#default_value' => $this->bundle() ?: key($this->bundleOptions()), '#disabled' => $this->isLocked()];
}
return $form;
}
示例8: addPageRoute
/**
* Returns the add page route.
*
* Built only for entity types that have bundles.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function addPageRoute(EntityTypeInterface $entity_type)
{
if ($entity_type->hasLinkTemplate('add-page') && $entity_type->getKey('bundle')) {
$route = new Route($entity_type->getLinkTemplate('add-page'));
$route->setDefault('_controller', '\\Drupal\\entity\\Controller\\EntityCreateController::addPage');
$route->setDefault('_title_callback', '\\Drupal\\entity\\Controller\\EntityCreateController::addPageTitle');
$route->setDefault('entity_type_id', $entity_type->id());
$route->setRequirement('_entity_create_access', $entity_type->id());
return $route;
}
}
示例9: getEntity
/**
* Returns a mock entity for testing.
*
* @param string $class
* The class name to mock. Should be \Drupal\Core\Entity\Entity or a
* subclass.
* @param array $values
* An array of entity values to construct the mock entity with.
* @param array $methods
* (optional) An array of additional methods to mock on the entity object.
* The getEntityType() and entityManager() methods are always mocked.
*
* @return \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getEntity($class, array $values, array $methods = [])
{
$methods = array_merge($methods, ['getEntityType', 'entityManager']);
// Prophecy does not allow prophesizing abstract classes while actually
// calling their code. We use Prophecy below because that allows us to
// add method prophecies later while still revealing the prophecy now.
$entity = $this->getMockBuilder($class)->setConstructorArgs([$values, $this->entityTypeId])->setMethods($methods)->getMockForAbstractClass();
$this->entityType = $this->prophesize(EntityTypeInterface::class);
$this->entityType->getLinkTemplates()->willReturn([]);
$this->entityType->getKey('langcode')->willReturn(FALSE);
$entity->method('getEntityType')->willReturn($this->entityType->reveal());
$this->entityManager = $this->prophesize(EntityManagerInterface::class);
$entity->method('entityManager')->willReturn($this->entityManager->reveal());
return $entity;
}
示例10: getReferenceableEntities
/**
* Gets the entities that can be filtered by.
*
* @return \Drupal\Core\Entity\EntityInterface[]
*/
protected function getReferenceableEntities() {
if ($this->referenceableEntities !== NULL) {
return $this->referenceableEntities;
}
$target_ids = NULL;
// Filter by bundle if if the plugin was configured to do so.
$target_bundles = array_filter($this->options['verf_target_bundles']);
if ($this->targetEntityType->hasKey('bundle') && $target_bundles) {
$query = $this->targetEntityStorage->getQuery();
$query->condition($this->targetEntityType->getKey('bundle'), $target_bundles, 'IN');
$target_ids = $query->execute();
}
$this->referenceableEntities = $this->targetEntityStorage->loadMultiple($target_ids);
return $this->referenceableEntities;
}
示例11: getTableFields
/**
* {@inheritdoc}
*/
public function getTableFields($bundles)
{
$definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType->id());
$label_key = $this->entityType->getKey('label');
$label_field_label = t('Label');
if ($label_key && isset($definitions[$label_key])) {
$label_field_label = $definitions[$label_key]->getLabel();
}
$bundle_key = $this->entityType->getKey('bundle');
$bundle_field_label = t('Type');
if ($bundle_key && isset($definitions[$bundle_key])) {
$bundle_field_label = $definitions[$bundle_key]->getLabel();
}
$fields = [];
$fields['label'] = ['type' => 'label', 'label' => $label_field_label, 'weight' => 1];
if (count($bundles) > 1) {
$fields[$bundle_key] = ['type' => 'field', 'label' => $bundle_field_label, 'weight' => 2, 'display_options' => ['type' => 'entity_reference_label', 'settings' => ['link' => FALSE]]];
}
return $fields;
}
示例12: getEntityTypeIdKeyType
/**
* Gets the type of the ID key for a given entity type.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* An entity type.
*
* @return string|null
* The type of the ID key for a given entity type, or NULL if the entity
* type does not support fields.
*/
protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type)
{
if (!$entity_type->isSubclassOf(FieldableEntityInterface::class)) {
return NULL;
}
$field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
return $field_storage_definitions[$entity_type->getKey('id')]->getType();
}
示例13: entityFormAlter
/**
* {@inheritdoc}
*/
public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity)
{
$form_object = $form_state->getFormObject();
$form_langcode = $form_object->getFormLangcode($form_state);
$entity_langcode = $entity->getUntranslated()->language()->getId();
$source_langcode = $this->getSourceLangcode($form_state);
$new_translation = !empty($source_langcode);
$translations = $entity->getTranslationLanguages();
if ($new_translation) {
// Make sure a new translation does not appear as existing yet.
unset($translations[$form_langcode]);
}
$is_translation = !$form_object->isDefaultFormLangcode($form_state);
$has_translations = count($translations) > 1;
// Adjust page title to specify the current language being edited, if we
// have at least one translation.
$languages = $this->languageManager->getLanguages();
if (isset($languages[$form_langcode]) && ($has_translations || $new_translation)) {
$title = $this->entityFormTitle($entity);
// When editing the original values display just the entity label.
if ($form_langcode != $entity_langcode) {
$t_args = array('%language' => $languages[$form_langcode]->getName(), '%title' => $entity->label(), '!title' => $title);
$title = empty($source_langcode) ? t('!title [%language translation]', $t_args) : t('Create %language translation of %title', $t_args);
}
$form['#title'] = $title;
}
// Display source language selector only if we are creating a new
// translation and there are at least two translations available.
if ($has_translations && $new_translation) {
$form['source_langcode'] = array('#type' => 'details', '#title' => t('Source language: @language', array('@language' => $languages[$source_langcode]->getName())), '#tree' => TRUE, '#weight' => -100, '#multilingual' => TRUE, 'source' => array('#title' => t('Select source language'), '#title_display' => 'invisible', '#type' => 'select', '#default_value' => $source_langcode, '#options' => array()), 'submit' => array('#type' => 'submit', '#value' => t('Change'), '#submit' => array(array($this, 'entityFormSourceChange'))));
foreach ($this->languageManager->getLanguages() as $language) {
if (isset($translations[$language->getId()])) {
$form['source_langcode']['source']['#options'][$language->getId()] = $language->getName();
}
}
}
// Locate the language widget.
$langcode_key = $this->entityType->getKey('langcode');
if (isset($form[$langcode_key])) {
$language_widget =& $form[$langcode_key];
}
// If we are editing the source entity, limit the list of languages so that
// it is not possible to switch to a language for which a translation
// already exists. Note that this will only work if the widget is structured
// like \Drupal\Core\Field\Plugin\Field\FieldWidget\LanguageSelectWidget.
if (isset($language_widget['widget'][0]['value']) && !$is_translation && $has_translations) {
$language_select =& $language_widget['widget'][0]['value'];
if ($language_select['#type'] == 'language_select') {
$options = array();
foreach ($this->languageManager->getLanguages() as $language) {
// Show the current language, and the languages for which no
// translation already exists.
if (empty($translations[$language->getId()]) || $language->getId() == $entity_langcode) {
$options[$language->getId()] = $language->getName();
}
}
$language_select['#options'] = $options;
}
}
if ($is_translation) {
if (isset($language_widget)) {
$language_widget['widget']['#access'] = FALSE;
}
// Replace the delete button with the delete translation one.
if (!$new_translation) {
$weight = 100;
foreach (array('delete', 'submit') as $key) {
if (isset($form['actions'][$key]['weight'])) {
$weight = $form['actions'][$key]['weight'];
break;
}
}
$access = $this->getTranslationAccess($entity, 'delete')->isAllowed() || $entity->access('delete') && $this->entityType->hasLinkTemplate('delete-form');
$form['actions']['delete_translation'] = array('#type' => 'submit', '#value' => t('Delete translation'), '#weight' => $weight, '#submit' => array(array($this, 'entityFormDeleteTranslation')), '#access' => $access);
}
// Always remove the delete button on translation forms.
unset($form['actions']['delete']);
}
// We need to display the translation tab only when there is at least one
// translation available or a new one is about to be created.
if ($new_translation || $has_translations) {
$form['content_translation'] = array('#type' => 'details', '#title' => t('Translation'), '#tree' => TRUE, '#weight' => 10, '#access' => $this->getTranslationAccess($entity, $source_langcode ? 'create' : 'update')->isAllowed(), '#multilingual' => TRUE);
// A new translation is enabled by default.
$metadata = $this->manager->getTranslationMetadata($entity);
$status = $new_translation || $metadata->isPublished();
// If there is only one published translation we cannot unpublish it,
// since there would be nothing left to display.
$enabled = TRUE;
if ($status) {
$published = 0;
foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$published += $this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished();
}
$enabled = $published > 1;
}
$description = $enabled ? t('An unpublished translation will not be visible without translation permissions.') : t('Only this translation is published. You must publish at least one more translation to unpublish this one.');
$form['content_translation']['status'] = array('#type' => 'checkbox', '#title' => t('This translation is published'), '#default_value' => $status, '#description' => $description, '#disabled' => !$enabled);
//.........这里部分代码省略.........
示例14: getBundles
/**
* Gets the bundles for the current entity field.
*
* If the view has a non-exposed bundle filter, the bundles are taken from
* there. Otherwise, the field's bundles are used.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The current entity type.
* @param string $field_name
* The current field name.
*
* @return string[]
* The bundles.
*/
protected function getBundles(EntityTypeInterface $entity_type, $field_name)
{
$bundles = [];
$bundle_key = $entity_type->getKey('bundle');
if ($bundle_key && isset($this->view->filter[$bundle_key])) {
$filter = $this->view->filter[$bundle_key];
if (!$filter->isExposed() && !empty($filter->value)) {
// 'all' is added by Views and isn't a bundle.
$bundles = array_diff($filter->value, ['all']);
}
}
// Fallback to the list of bundles the field is attached to.
if (empty($bundles)) {
$map = $this->entityFieldManager->getFieldMap();
$bundles = $map[$entity_type->id()][$field_name]['bundles'];
}
return $bundles;
}
示例15: testGetCanonicalRoute
/**
* @covers ::getCanonicalRoute
* @dataProvider providerTestGetCanonicalRoute
*/
public function testGetCanonicalRoute(Route $expected = NULL, EntityTypeInterface $entity_type, FieldStorageDefinitionInterface $field_storage_definition = NULL)
{
if ($field_storage_definition) {
$this->entityFieldManager->getFieldStorageDefinitions($entity_type->id())->willReturn([$entity_type->getKey('id') => $field_storage_definition]);
}
$route = $this->routeProvider->getCanonicalRoute($entity_type);
$this->assertEquals($expected, $route);
}