本文整理汇总了PHP中Drupal\Core\Field\FormatterBase::getSetting方法的典型用法代码示例。如果您正苦于以下问题:PHP FormatterBase::getSetting方法的具体用法?PHP FormatterBase::getSetting怎么用?PHP FormatterBase::getSetting使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Field\FormatterBase
的用法示例。
在下文中一共展示了FormatterBase::getSetting方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: captionSettings
/**
* Returns the form element for caption settings.
*
* @param \Drupal\Core\Field\FormatterBase $formatter
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
*
* @return array
*/
protected function captionSettings(FormatterBase $formatter, FieldDefinitionInterface $field_definition)
{
$field_settings = $field_definition->getSettings();
// Set the caption options.
$caption_options = array(0 => $formatter->t('None'), 1 => $formatter->t('Image title'), 'alt' => $formatter->t('Image ALT attribute'));
// Remove the options that are not available.
$action_fields = array();
if ($field_settings['title_field'] == FALSE) {
unset($caption_options[1]);
// User action required on the image title.
$action_fields[] = 'title';
}
if ($field_settings['alt_field'] == FALSE) {
unset($caption_options['alt']);
// User action required on the image alt.
$action_fields[] = 'alt';
}
// Create the caption element.
$element['caption'] = array('#title' => $formatter->t('Choose a caption source'), '#type' => 'select', '#options' => $caption_options);
// If the image field doesn't have all of the suitable caption sources, tell the user.
if ($action_fields) {
$action_text = $formatter->t('enable the @action_field field', array('@action_field' => join(' and/or ', $action_fields)));
/* This may be a base field definition (e.g. in Views UI) which means it
* is not associated with a bundle and will not have the toUrl() method.
* So we need to check for the existence of the method before we can
* build a link to the image field edit form.
*/
if (method_exists($field_definition, 'toUrl')) {
// Build the link to the image field edit form for this bundle.
$rel = "{$field_definition->getTargetEntityTypeId()}-field-edit-form";
$action = $field_definition->toLink($action_text, $rel, array('fragment' => 'edit-settings-alt-field', 'query' => \Drupal::destination()->getAsArray()))->toRenderable();
} else {
// Just use plain text if we can't build the field edit link.
$action = ['#markup' => $action_text];
}
$element['caption']['#description'] = $formatter->t('You need to @action for this image field to be able to use it as a caption.', array('@action' => render($action)));
// If there are no suitable caption sources, disable the caption element.
if (count($action_fields) >= 2) {
$element['caption']['#disabled'] = TRUE;
}
} else {
$element['caption']['#default_value'] = $formatter->getSetting('caption');
}
return $element;
}