diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index 6e98ae2..fa7817c 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -32,7 +32,8 @@ class LinkItem extends FieldItemBase implements LinkItemInterface { public static function defaultFieldSettings() { return array( 'title' => DRUPAL_OPTIONAL, - 'link_type' => LinkItemInterface::LINK_GENERIC + 'link_type' => LinkItemInterface::LINK_GENERIC, + 'content_types' => array() ) + parent::defaultFieldSettings(); } @@ -108,6 +109,20 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { DRUPAL_REQUIRED => t('Required'), ), ); + + // Get a list of content types. + $contentTypes = \Drupal::service('entity.manager')->getStorage('node_type')->loadMultiple(); + $contentTypesList = []; + foreach ($contentTypes as $contentType) { + $contentTypesList[$contentType->id()] = $contentType->label(); + } + // Add checkboxes for selecting content types to filter by. + $element['content_types'] = array( + '#type' => 'checkboxes', + '#title' => t('Content Types for Internal Links'), + '#default_value' => $this->getSetting('content_types'), + '#options' => $contentTypesList + ); return $element; } diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index fd670fd..80411a9 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -184,6 +184,31 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // @todo The user should be able to select an entity type. Will be fixed // in https://www.drupal.org/node/2423093. $element['uri']['#target_type'] = 'node'; + // If limiting content types are selected in the settings, update the query. + if ($content_types = $this->getFieldSetting('content_types')) { + // Massage array values into format selections settings array expects. + foreach ($content_types as $key => $type) { + if ($type == '0') { + unset($content_types[$key]); + } + else { + $content_types[$key] = $key; + } + } + // Update selections settings to include limited list of content types. + if (!empty($content_types)) { + $element['uri']['#selection_handler'] = 'default:node'; + $element['uri']['#selection_settings'] = [ + 'target_bundles' => $content_types, + 'sort' => [ + 'field' => '_none', + ], + 'auto_create' => FALSE, + 'auto_create_bundle' => FALSE, + 'match_operator' => 'CONTAINS', + ]; + } + } // Disable autocompletion when the first character is '/', '#' or '?'. $element['uri']['#attributes']['data-autocomplete-first-character-blacklist'] = '/#?';