function NavigationLinkBlock::blockForm

Overrides BlockPluginTrait::blockForm

File

core/modules/navigation/src/Plugin/Block/NavigationLinkBlock.php, line 40

Class

NavigationLinkBlock
Defines a link navigation block.

Namespace

Drupal\navigation\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) : array {
    $config = $this->configuration;
    $display_uri = NULL;
    if (!empty($config['uri'])) {
        try {
            // The current field value could have been entered by a different user.
            // However, if it is inaccessible to the current user, do not display it
            // to them.
            $url = Url::fromUri($config['uri']);
            if (\Drupal::currentUser()->hasPermission('link to any page') || $url->access()) {
                $display_uri = static::getUriAsDisplayableString($config['uri']);
            }
        } catch (\InvalidArgumentException) {
            // If $item->uri is invalid, show value as is, so the user can see what
            // to edit.
            $display_uri = $config['uri'];
        }
    }
    // @todo Logic related to the uri component has been borrowed from
    //   Drupal\link\Plugin\Field\FieldWidget\LinkWidget.
    //   Will be fixed in https://www.drupal.org/project/drupal/issues/3450518.
    $form['uri'] = [
        '#type' => 'entity_autocomplete',
        '#title' => $this->t('URL'),
        '#default_value' => $display_uri,
        '#element_validate' => [
            [
                static::class,
                'validateUriElement',
            ],
        ],
        '#attributes' => [
            'data-autocomplete-first-character-blacklist' => '/#?',
        ],
        // @todo The user should be able to select an entity type. Will be fixed
        //   in https://www.drupal.org/node/2423093.
'#target_type' => 'node',
        '#maxlength' => 2048,
        '#required' => TRUE,
        '#process_default_value' => FALSE,
    ];
    $form['title'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Link text'),
        '#default_value' => $config['title'],
        '#required' => TRUE,
        '#maxlength' => 255,
    ];
    $form['icon_class'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Icon CSS class'),
        '#default_value' => $config['icon_class'],
        '#element_validate' => [
            [
                static::class,
                'validateIconClassElement',
            ],
        ],
        '#required' => TRUE,
        '#maxlength' => 64,
    ];
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.