social_post_facebook-8.x-1.x-dev/src/Plugin/Action/Post.php

src/Plugin/Action/Post.php
<?php

namespace Drupal\social_post_facebook\Plugin\Action;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\eca\Plugin\Action\ConfigurableActionBase;

/**
 * Provides a 'Post' action.
 *
 * @Action(
 *   id = "social_post_facebook_post",
 *   label = @Translation("Post"),
 *   description = @Translation("Sends a post to a Facebook account"),
 * )
 */
class Post extends ConfigurableActionBase {

  /**
   * {@inheritdoc}
   */
  public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
    $result = AccessResult::allowed();
    return $return_as_object ? $result : $result->isAllowed();
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration(): array {
    return [
      'user_id' => 0,
      'message' => '',
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
    $form['user_id'] = [
      '#type' => 'number',
      '#title' => $this->t('Publish posts from this user.'),
      '#description' => $this->t('Provide the Drupal user id of the user who will publish posts to their facebook account.'),
      '#default_value' => $this->configuration['user_id'],
      '#eca_token_replacement' => TRUE,
    ];

    $form['message'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Message'),
      '#description' => $this->t('Provide the message that should be sent to Facebook.'),
      '#default_value' => $this->configuration['message'],
      '#eca_token_replacement' => TRUE,
    ];
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
    $this->configuration['user_id'] = $form_state->getValue('user_id');
    $this->configuration['message'] = $form_state->getValue('message');
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    // $post_manager = \Drupal::getContainer('facebook_post.manager');
    /** @var \Drupal\social_post_facebook\FacebookPostAuthManager $post_manager */
    $post_manager = \Drupal::service('facebook_post.social_post_auth_manager');
    $token = $this->tokenService;
    $message = $this->configuration['message'];
    $message = $token->getOrReplace($message);
    $user_id = $this->configuration['user_id'];
    $user_id = $token->getOrReplace($user_id);

    $post_manager->doPost($message, $user_id);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc