Commit 6c0e119c authored by mxh's avatar mxh
Browse files

Issue #3254007 by mxh: Use "bundle" as default token type

parent 16164030
Loading
Loading
Loading
Loading
+14 −6
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ function field_bundle_token_info() {
  $type = [
    'name' => t('Field bundles'),
    'description' => t('Tokens related to individual field bundle items.'),
    'needs-data' => 'field-bundle',
    'needs-data' => 'bundle',
  ];

  $field_bundle = [];
@@ -71,8 +71,8 @@ function field_bundle_token_info() {
  ];

  return [
    'types' => ['field-bundle' => $type],
    'tokens' => ['field-bundle' => $field_bundle],
    'types' => ['bundle' => $type],
    'tokens' => ['bundle' => $field_bundle],
  ];
}

@@ -92,9 +92,17 @@ function field_bundle_tokens($type, $tokens, array $data, array $options, Bubble
  }
  $replacements = [];

  if ($type == 'field-bundle' && !empty($data['field-bundle'])) {
  /** @var \Drupal\field_bundle\FieldBundleInterface $field_bundle */
    $field_bundle = $data['field-bundle'];
  $field_bundle = NULL;
  $allowed_keys = ['bundle', 'field_bundle', 'field-bundle'];
  foreach ($allowed_keys as $key) {
    if (isset($data[$key])) {
      $field_bundle = $data[$key];
      break;
    }
  }

  if (in_array($type, $allowed_keys) && $field_bundle) {
    if (isset($options['langcode']) && $field_bundle->language()->getId() !== $options['langcode'] && $field_bundle->hasTranslation($options['langcode'])) {
      $field_bundle = $field_bundle->getTranslation($options['langcode']);
    }
+2 −2
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ use Drupal\user\EntityOwnerTrait;
 *   field_ui_base_route = "entity.field_bundle_config.edit_form",
 *   common_reference_target = TRUE,
 *   permission_granularity = "bundle",
 *   token_type = "field-bundle"
 *   token_type = "bundle"
 * )
 */
class FieldBundle extends EditorialContentEntityBase implements FieldBundleInterface {
@@ -200,7 +200,7 @@ class FieldBundle extends EditorialContentEntityBase implements FieldBundleInter
      }
    }
    if (!empty($label_pattern)) {
      $this->label->value = \Drupal::token()->replace($label_pattern, ['field-bundle' => $this], ['langcode' => $this->language()->getId()]);
      $this->label->value = \Drupal::token()->replace($label_pattern, ['bundle' => $this], ['langcode' => $this->language()->getId()]);
    }
  }

+1 −1
Original line number Diff line number Diff line
@@ -98,7 +98,7 @@ class FieldBundleConfig extends ConfigEntityBundleBase implements FieldBundleCon
   *
   * @var string
   */
  protected $label_pattern = '[field-bundle:string-representation]';
  protected $label_pattern = '[bundle:string-representation]';

  /**
   * {@inheritdoc}
+15 −1
Original line number Diff line number Diff line
@@ -90,12 +90,26 @@ class FieldBundleConfigForm extends BundleEntityFormBase {
    $form['label_pattern'] = [
      '#title' => $this->t('Label pattern'),
      '#type' => 'textfield',
      '#description' => $this->t('Define a pattern to use for creating the label of the field bundle. Tokens are allowed, e.g. [field-bundle:config-label]. Leave empty to not use any pattern.'),
      '#description' => $this->t('Define a pattern to use for creating the label of the field bundle. Tokens are allowed, e.g. [bundle:config-label]. Leave empty to not use any pattern.'),
      '#default_value' => $bundle_config->getLabelPattern(),
      '#size' => 255,
      '#maxlength' => 255,
    ];

    if ($this->moduleHandler->moduleExists('token')) {
      $form['label_pattern_help'] = [
        '#type' => 'container',
        'token_link' => [
          '#theme' => 'token_tree_link',
          '#token_types' => ['bundle'],
          '#dialog' => TRUE,
        ],
      ];
    }
    else {
      $form['label_pattern']['#description'] .= ' ' . $this->t('To get a list of available tokens, install the <a target="_blank" rel="noreferrer noopener" href=":drupal-token" target="blank">contrib Token</a> module.', [':drupal-token' => 'https://www.drupal.org/project/token']);
    }

    $form['additional_settings'] = [
      '#type' => 'vertical_tabs',
    ];