本文整理汇总了PHP中Drupal\Core\Field\FieldDefinitionInterface类的典型用法代码示例。如果您正苦于以下问题:PHP FieldDefinitionInterface类的具体用法?PHP FieldDefinitionInterface怎么用?PHP FieldDefinitionInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FieldDefinitionInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generateSampleValue
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition)
{
$random = new Random();
$max = $field_definition->getSetting('max_length');
$values['value'] = $random->word(mt_rand(1, $max));
return $values;
}
示例2: generateSampleValue
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition)
{
$min = $field_definition->getSetting('min') ?: 0;
$max = $field_definition->getSetting('max') ?: 999;
$values['value'] = mt_rand($min, $max);
return $values;
}
示例3: generateSampleValue
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition)
{
$random = new Random();
if ($field_definition->getItemDefinition()->getSetting('link_type') & LinkItemInterface::LINK_EXTERNAL) {
// Set of possible top-level domains.
$tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info');
// Set random length for the domain name.
$domain_length = mt_rand(7, 15);
switch ($field_definition->getSetting('title')) {
case DRUPAL_DISABLED:
$values['title'] = '';
break;
case DRUPAL_REQUIRED:
$values['title'] = $random->sentences(4);
break;
case DRUPAL_OPTIONAL:
// In case of optional title, randomize its generation.
$values['title'] = mt_rand(0, 1) ? $random->sentences(4) : '';
break;
}
$values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, sizeof($tlds) - 1)];
} else {
$values['uri'] = 'base:' . $random->name(mt_rand(1, 64));
}
return $values;
}
示例4: checkFieldAccess
/**
* {@inheritdoc}
*/
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL)
{
if ($operation == 'edit') {
// Only users with the "administer comments" permission can edit
// administrative fields.
$administrative_fields = array('uid', 'status', 'created', 'date');
if (in_array($field_definition->getName(), $administrative_fields, TRUE)) {
return AccessResult::allowedIfHasPermission($account, 'administer comments');
}
// No user can change read-only fields.
$read_only_fields = array('hostname', 'changed', 'cid', 'thread');
// These fields can be edited during comment creation.
$create_only_fields = ['comment_type', 'uuid', 'entity_id', 'entity_type', 'field_name', 'pid'];
if ($items && ($entity = $items->getEntity()) && $entity->isNew() && in_array($field_definition->getName(), $create_only_fields, TRUE)) {
// We are creating a new comment, user can edit create only fields.
return AccessResult::allowedIfHasPermission($account, 'post comments')->addCacheableDependency($entity);
}
// We are editing an existing comment - create only fields are now read
// only.
$read_only_fields = array_merge($read_only_fields, $create_only_fields);
if (in_array($field_definition->getName(), $read_only_fields, TRUE)) {
return AccessResult::forbidden();
}
// If the field is configured to accept anonymous contact details - admins
// can edit name, homepage and mail. Anonymous users can also fill in the
// fields on comment creation.
if (in_array($field_definition->getName(), ['name', 'mail', 'homepage'], TRUE)) {
if (!$items) {
// We cannot make a decision about access to edit these fields if we
// don't have any items and therefore cannot determine the Comment
// entity. In this case we err on the side of caution and prevent edit
// access.
return AccessResult::forbidden();
}
/** @var \Drupal\comment\CommentInterface $entity */
$entity = $items->getEntity();
$commented_entity = $entity->getCommentedEntity();
$anonymous_contact = $commented_entity->get($entity->getFieldName())->getFieldDefinition()->getSetting('anonymous');
$admin_access = AccessResult::allowedIfHasPermission($account, 'administer comments');
$anonymous_access = AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT && $account->hasPermission('post comments'))->cachePerPermissions()->cacheUntilEntityChanges($entity)->cacheUntilEntityChanges($field_definition->getConfig($commented_entity->bundle()))->cacheUntilEntityChanges($commented_entity);
return $admin_access->orIf($anonymous_access);
}
}
if ($operation == 'view') {
$entity = $items ? $items->getEntity() : NULL;
// Admins can view any fields except hostname, other users need both the
// "access comments" permission and for the comment to be published. The
// mail field is hidden from non-admins.
$admin_access = AccessResult::allowedIf($account->hasPermission('administer comments') && $field_definition->getName() != 'hostname')->cachePerPermissions();
$anonymous_access = AccessResult::allowedIf($account->hasPermission('access comments') && (!$entity || $entity->isPublished()) && !in_array($field_definition->getName(), array('mail', 'hostname'), TRUE))->cachePerPermissions();
if ($entity) {
$anonymous_access->cacheUntilEntityChanges($entity);
}
return $admin_access->orIf($anonymous_access);
}
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}
示例5: checkFieldAccess
/**
* {@inheritdoc}
*/
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL)
{
// No user can edit the status of a file. Prevents saving a new file as
// persistent before even validating it.
if ($field_definition->getName() === 'status' && $operation === 'edit') {
return AccessResult::forbidden();
}
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}
示例6: prepareTarget
/**
* {@inheritdoc}
*/
protected static function prepareTarget(FieldDefinitionInterface $field_definition)
{
// Only reference content entities. Configuration entities will need custom
// targets.
$type = $field_definition->getSetting('target_type');
if (!\Drupal::entityManager()->getDefinition($type)->isSubclassOf('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {
return;
}
return FieldTargetDefinition::createFromFieldDefinition($field_definition)->addProperty('target_id');
}
示例7: isApplicable
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition)
{
$entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
$widget = $entity_form_display->getRenderer($field_definition->getName());
$widget_id = $widget->getBaseId();
if ($field_definition->isList() && $widget_id == 'video_upload') {
return TRUE;
}
return FALSE;
}
示例8: checkFieldAccess
/**
* {@inheritdoc}
*/
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL)
{
// Fields that are not implicitly allowed to administrative users.
$explicit_check_fields = array('pass');
// Administrative users are allowed to edit and view all fields.
if (!in_array($field_definition->getName(), $explicit_check_fields) && $account->hasPermission('administer users')) {
return AccessResult::allowed()->cachePerPermissions();
}
// Flag to indicate if this user entity is the own user account.
$is_own_account = $items ? $items->getEntity()->id() == $account->id() : FALSE;
switch ($field_definition->getName()) {
case 'name':
// Allow view access to anyone with access to the entity. Anonymous
// users should be able to access the username field during the
// registration process, otherwise the username and email constraints
// are not checked.
if ($operation == 'view' || $items && $account->isAnonymous() && $items->getEntity()->isAnonymous()) {
return AccessResult::allowed()->cachePerPermissions();
}
// Allow edit access for the own user name if the permission is
// satisfied.
if ($is_own_account && $account->hasPermission('change own username')) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser();
} else {
return AccessResult::forbidden();
}
case 'preferred_langcode':
case 'preferred_admin_langcode':
case 'timezone':
case 'mail':
// Allow view access to own mail address and other personalization
// settings.
if ($operation == 'view') {
return $is_own_account ? AccessResult::allowed()->cachePerUser() : AccessResult::forbidden();
}
// Anyone that can edit the user can also edit this field.
return AccessResult::allowed()->cachePerPermissions();
case 'pass':
// Allow editing the password, but not viewing it.
return $operation == 'edit' ? AccessResult::allowed() : AccessResult::forbidden();
case 'created':
// Allow viewing the created date, but not editing it.
return $operation == 'view' ? AccessResult::allowed() : AccessResult::forbidden();
case 'roles':
case 'status':
case 'access':
case 'login':
case 'init':
return AccessResult::forbidden();
}
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}
示例9: generateSampleValue
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition)
{
$type = $field_definition->getSetting('datetime_type');
// Just pick a date in the past year. No guidance is provided by this Field
// type.
$timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365);
if ($type == DateTimeItem::DATETIME_TYPE_DATE) {
$values['value'] = gmdate(DATETIME_DATE_STORAGE_FORMAT, $timestamp);
} else {
$values['value'] = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, $timestamp);
}
return $values;
}
示例10: generateSampleValue
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition)
{
$settings = $field_definition->getSettings();
$precision = rand(10, 32);
$scale = rand(0, 2);
$max = is_numeric($settings['max']) ?: pow(10, $precision - $scale) - 1;
$min = is_numeric($settings['min']) ?: -pow(10, $precision - $scale) + 1;
// @see "Example #1 Calculate a random floating-point number" in
// http://php.net/manual/function.mt-getrandmax.php
$random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
$values['value'] = self::truncateDecimal($random_decimal, $scale);
return $values;
}
示例11: generateSampleValue
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition)
{
$random = new Random();
$settings = $field_definition->getSettings();
if (empty($settings['max_length'])) {
// Textarea handling
$value = $random->paragraphs();
} else {
// Textfield handling.
$value = substr($random->sentences(mt_rand(1, $settings['max_length'] / 3), FALSE), 0, $settings['max_length']);
}
$values = array('value' => $value, 'summary' => $value, 'format' => filter_fallback_format());
return $values;
}
示例12: isApplicable
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition)
{
// This formatter is only available for entity types that have a view
// builder.
$target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
return \Drupal::entityManager()->getDefinition($target_type)->hasViewBuilderClass();
}
示例13: settingsForm
/**
* {@inheritdoc}
*/
public static function settingsForm(FieldDefinitionInterface $field_definition)
{
$selection_handler_settings = $field_definition->getSetting('handler_settings');
// Merge in default values.
$selection_handler_settings += array('filter' => array('type' => '_none'));
// Add user specific filter options.
$form['filter']['type'] = array('#type' => 'select', '#title' => t('Filter by'), '#options' => array('_none' => t('- None -'), 'role' => t('User role')), '#ajax' => TRUE, '#limit_validation_errors' => array(), '#default_value' => $selection_handler_settings['filter']['type']);
$form['filter']['settings'] = array('#type' => 'container', '#attributes' => array('class' => array('entity_reference-settings')), '#process' => array('_entity_reference_form_process_merge_parent'));
if ($selection_handler_settings['filter']['type'] == 'role') {
// Merge in default values.
$selection_handler_settings['filter'] += array('role' => NULL);
$form['filter']['settings']['role'] = array('#type' => 'checkboxes', '#title' => t('Restrict to the selected roles'), '#required' => TRUE, '#options' => array_diff_key(user_role_names(TRUE), array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)), '#default_value' => $selection_handler_settings['filter']['role']);
}
$form += parent::settingsForm($field_definition);
return $form;
}
示例14: isDestinationFieldCompatible
/**
* Check if a field on the entity type to update is a possible destination field.
*
* @todo Should this be on our FieldManager service?
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
* Field definition on entity type to update to check.
* @param \Drupal\Core\Field\FieldDefinitionInterface $source_field
* Source field to check compatibility against. If none then check generally.
*
* @return bool
*/
protected function isDestinationFieldCompatible(FieldStorageDefinitionInterface $definition, FieldDefinitionInterface $source_field = NULL)
{
// @todo Create field definition wrapper class to treat FieldDefinitionInterface and FieldStorageDefinitionInterface the same.
if ($definition instanceof BaseFieldDefinition && $definition->isReadOnly()) {
return FALSE;
}
// Don't allow updates on updates!
if ($definition->getType() == 'entity_reference') {
if ($definition->getSetting('target_type') == 'scheduled_update') {
return FALSE;
}
}
if ($source_field) {
$matching_types = $this->getMatchingFieldTypes($source_field->getType());
if (!in_array($definition->getType(), $matching_types)) {
return FALSE;
}
// Check cardinality
$destination_cardinality = $definition->getCardinality();
$source_cardinality = $source_field->getFieldStorageDefinition()->getCardinality();
// $destination_cardinality is unlimited. It doesn't matter what source is.
if ($destination_cardinality != -1) {
if ($source_cardinality == -1) {
return FALSE;
}
if ($source_cardinality > $destination_cardinality) {
return FALSE;
}
}
switch ($definition->getType()) {
case 'entity_reference':
// Entity reference field must match entity target types.
if ($definition->getSetting('target_type') != $source_field->getSetting('target_type')) {
return FALSE;
}
// @todo Check bundles
break;
// @todo Other type specific conditions?
}
}
return TRUE;
}
示例15: getOptions
public function getOptions(FieldDefinitionInterface $field, $component)
{
$fs = $field->getFieldStorageDefinition()->getSettings();
$options = $fs[$component . '_options'];
foreach ($options as $index => $opt) {
if (preg_match('/^\\[vocabulary:([0-9a-z\\_]{1,})\\]/', trim($opt), $matches)) {
unset($options[$index]);
if ($this->termStorage && $this->vocabularyStorage) {
$vocabulary = $this->vocabularyStorage->load($matches[1]);
if ($vocabulary) {
$max_length = isset($fs['max_length'][$component]) ? $fs['max_length'][$component] : 255;
foreach ($this->termStorage->loadTree($vocabulary->id()) as $term) {
if (Unicode::strlen($term->name) <= $max_length) {
$options[] = $term->name;
}
}
}
}
}
}
// Options could come from multiple sources, filter duplicates.
$options = array_unique($options);
if (isset($fs['sort_options']) && !empty($fs['sort_options'][$component])) {
natcasesort($options);
}
$default = FALSE;
foreach ($options as $index => $opt) {
if (strpos($opt, '--') === 0) {
unset($options[$index]);
$default = trim(Unicode::substr($opt, 2));
}
}
$options = array_map('trim', $options);
$options = array_combine($options, $options);
if ($default !== FALSE) {
$options = array('' => $default) + $options;
}
return $options;
}