You are here

function block_token_form_block_form_alter in Block Token 8

Implements hook_form_FORM_ID_alter().

File

./block_token.module, line 159
Defines necessary hooks and functions for block_token form.

Code

function block_token_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (\Drupal::currentUser()
    ->hasPermission('administer block token')) {

    /** @var \Drupal\block\BlockInterface $block */
    $block = $form_state
      ->getFormObject()
      ->getEntity();

    // This will automatically be saved in the third party settings.
    $form['third_party_settings']['#tree'] = TRUE;
    $form['third_party_settings']['block_token']['token_value'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create the token for this block'),
      '#description' => t('Token string is not available until the block is saved.'),
      '#default_value' => $block
        ->getThirdPartySetting('block_token', 'token_value'),
    );
    $id = $form['id']['#default_value'];
    $module = $form['settings']['provider']['#value'];
    $token = block_token_token_name($module, $id);
    if ($token) {
      if (!$block
        ->getThirdPartySetting('block_token', 'token_value')) {

        // When token checkbox is not checked.
        $form['third_party_settings']['block_token']['token_value']['#description'] = t('Token will be @token', array(
          '@token' => sprintf('[block_token:%s]', $token),
        ));
      }
      else {
        $form['third_party_settings']['block_token']['token_value']['#description'] = t('Token is @token', array(
          '@token' => sprintf('[block_token:%s]', $token),
        ));
      }
    }
  }
}