You are here

class ShareMessageForm in Share Message 8

Base form controller for Share Message edit forms.

Hierarchy

Expanded class hierarchy of ShareMessageForm

File

src/Form/ShareMessageForm.php, line 18

Namespace

Drupal\sharemessage\Form
View source
class ShareMessageForm extends EntityForm {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Share plugin manager.
   *
   * @var \Drupal\sharemessage\SharePluginManager
   */
  protected $sharePluginManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity type bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * Constructs a new ShareMessageForm object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   * The module handler.
   * @param \Drupal\sharemessage\SharePluginManager $share_manager
   *   The share manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info.
   */
  public function __construct(ModuleHandlerInterface $module_handler, SharePluginManager $share_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
    $this->moduleHandler = $module_handler;
    $this->sharePluginManager = $share_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('module_handler'), $container
      ->get('plugin.manager.sharemessage.share'), $container
      ->get('entity_type.manager'), $container
      ->get('entity_type.bundle.info'));
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::form().
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    /** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
    $sharemessage = $this->entity;
    $defaults = \Drupal::config('sharemessage.settings');
    $available = $this->sharePluginManager
      ->getLabels();
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#required' => TRUE,
      '#default_value' => $sharemessage
        ->label(),
      '#weight' => -3,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#title' => t('Machine Name'),
      '#machine_name' => [
        'exists' => 'sharemessage_check_machine_name_if_exist',
        'source' => [
          'label',
        ],
      ],
      '#required' => TRUE,
      '#weight' => -2,
      '#disabled' => !$sharemessage
        ->isNew(),
      '#default_value' => $sharemessage
        ->id(),
    ];
    $form['title'] = [
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $sharemessage->title,
      '#description' => t('Used as title in the Share Message, where applicable: Facebook, E-Mail subject, ...'),
      '#weight' => 5,
    ];
    $form['message_long'] = [
      '#type' => 'textarea',
      '#title' => t('Long Description'),
      '#default_value' => $sharemessage->message_long,
      '#description' => t('Used as long description for the Share Message, where applicable: Facebook, Email body, ...'),
      '#weight' => 10,
    ];
    $form['message_short'] = [
      '#type' => 'textfield',
      '#title' => t('Short Description'),
      '#default_value' => $sharemessage->message_short,
      '#description' => t('Used as short description for twitter messages.'),
      '#weight' => 15,
    ];
    $form['video_url'] = [
      '#type' => 'textfield',
      '#title' => t('Video URL'),
      '#default_value' => $sharemessage->video_url,
      '#description' => t('The video URL that will be used for sharing.'),
      '#weight' => 18,
    ];
    $form['image_url'] = [
      '#type' => 'textfield',
      '#title' => t('Image URL'),
      '#default_value' => $sharemessage->image_url,
      '#description' => t('The image URL that will be used for sharing. If a video URL is set, the image is used as a thumbnail for the video.'),
      '#weight' => 20,
    ];
    $form['image_width'] = [
      '#type' => 'textfield',
      '#title' => t('Image Width'),
      '#default_value' => $sharemessage->image_width,
      '#description' => t('The width of the image that will be used for sharing.'),
      '#weight' => 21,
    ];
    $form['image_height'] = [
      '#type' => 'textfield',
      '#title' => t('Image Height'),
      '#default_value' => $sharemessage->image_height,
      '#description' => t('The height of the image that will be used for sharing.'),
      '#weight' => 22,
    ];

    // @todo: Convert this to a file upload/selection widget.
    $form['fallback_image'] = [
      '#type' => 'textfield',
      '#title' => t('Fallback image (File UUID)'),
      '#default_value' => $sharemessage->fallback_image,
      '#description' => t('Specify a static fallback image that is used if the Image URL is empty (For example, when tokens are used and the specified image field is empty).'),
      '#weight' => 23,
    ];
    $form['share_url'] = [
      '#type' => 'textfield',
      '#title' => t('Shared URL'),
      '#default_value' => $sharemessage->share_url,
      '#description' => t('Specific URL that will be shared, defaults to the current page.'),
      '#weight' => 25,
    ];

    // If the Share Message plugin is not set, pick AddThis plugin as the
    // default.
    if (!$sharemessage
      ->hasPlugin()) {
      $sharemessage
        ->setPluginID('addthis');
    }
    $form['plugin_wrapper'] = [
      '#type' => 'container',
      '#prefix' => '<div id="sharemessage-plugin-wrapper">',
      '#suffix' => '</div>',
    ];
    $definition = $this->sharePluginManager
      ->getDefinition($sharemessage
      ->getPluginID());
    if ($sharemessage
      ->hasPlugin()) {
      $form['plugin_wrapper']['plugin'] = [
        '#type' => 'select',
        '#title' => t('Share Message plugin'),
        '#description' => isset($definition['description']) ? Xss::filter($definition['description']) : '',
        '#options' => $available,
        '#default_value' => $sharemessage
          ->getPluginID(),
        '#required' => TRUE,
        '#ajax' => [
          'callback' => [
            $this,
            'ajaxShareMessagePluginSelect',
          ],
          'wrapper' => 'sharemessage-plugin-wrapper',
        ],
      ];
      $form['plugin_wrapper']['plugin_select'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Select plugin'),
        '#submit' => [
          '::ajaxShareMessagePluginSelect',
        ],
        '#attributes' => [
          'class' => [
            'js-hide',
          ],
        ],
      ];
      $form['plugin_wrapper']['settings'] = [
        '#type' => 'details',
        '#title' => t('@plugin plugin settings', [
          '@plugin' => $definition['label'],
        ]),
        '#tree' => TRUE,
        '#open' => TRUE,
      ];

      // Add the Share Message plugin settings form.
      $form['plugin_wrapper']['settings'] += $sharemessage
        ->getPlugin()
        ->buildConfigurationForm($form['plugin_wrapper']['settings'], $form_state);
      if (!Element::children($form['plugin_wrapper']['settings'])) {

        // Settings fieldset.
        $form['plugin_wrapper']['settings']['override_default_settings'] = [
          '#type' => 'item',
          '#description' => t("The @plugin plugin doesn't provide any settings.", [
            '@plugin' => $sharemessage
              ->getPluginDefinition()['label'],
          ]),
        ];
      }
    }
    if ($defaults
      ->get('message_enforcement')) {
      $form['enforce_usage'] = [
        '#type' => 'checkbox',
        '#title' => t('Enforce the usage of this Share Message on the page it points to'),
        '#description' => t('If checked, this Share Message will be used on the page that it is referring to and override the Share Message there.'),
        '#default_value' => $sharemessage->enforce_usage ?: 0,
        '#weight' => 40,
      ];
    }

    // Define a form to expose a Share Message as an extra field for a given
    // entity type and its bundle(s).
    $form['sharemessage_extra_field'] = [
      '#type' => 'container',
      '#prefix' => '<div id="sharemessage-extra-field">',
      '#suffix' => '</div>',
      '#weight' => 50,
    ];

    // If the entity type ajax has been triggered, store the new entity type
    // used to display its bundles on the UI.
    $current_type = $sharemessage
      ->getExtraFieldEntityType();
    if ($form_state
      ->hasValue('entity_type') && $form_state
      ->getValue('entity_type') !== $current_type) {
      $sharemessage
        ->setExtraFieldEntityType($form_state
        ->getValue('entity_type'));
      $current_type = $sharemessage
        ->getExtraFieldEntityType();
    }

    // Get the entity types that have view builder.
    $extra_field_entity_type_options = [];
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type_id => $definition) {
      if ($definition
        ->getGroup() == 'content' && $definition
        ->hasHandlerClass('view_builder')) {

        // Check whether entity type has any view displays.
        $view_display_ids = \Drupal::entityQuery('entity_view_display')
          ->condition('targetEntityType', $entity_type_id)
          ->count()
          ->execute();
        if ($view_display_ids) {
          $extra_field_entity_type_options[$entity_type_id] = $definition
            ->getLabel();
        }
      }
    }

    // Get the bundles of the selected content entity type.
    $extra_field_bundle_options = [];
    if ($current_type) {
      $extra_field_bundle_options = $this
        ->getBundles($current_type);
    }

    // Define the entity type select form.
    $form['sharemessage_extra_field']['entity_type'] = [
      '#type' => 'select',
      '#title' => t('Share Message extra field'),
      '#description' => t('Select an entity type, then check the content entities where you want to show this Share Message. This only displays entity types which have at least one view display configured. In case an entity type does not show up, go to "Manage display" and press "Save".'),
      '#options' => $extra_field_entity_type_options,
      '#empty_option' => t('- None -'),
      '#empty_value' => '',
      '#default_value' => $current_type,
      '#required' => FALSE,
      '#ajax' => [
        'callback' => [
          $this,
          'ajaxShareMessageContentTypeSelect',
        ],
        'wrapper' => 'sharemessage-extra-field',
      ],
    ];
    $form['sharemessage_extra_field']['entity_type_select'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Select entity type'),
      '#submit' => [
        '::ajaxShareMessageContentTypeSelect',
      ],
      '#attributes' => [
        'class' => [
          'js-hide',
        ],
      ],
    ];

    // Show the selected entity type's bundles, if there are any. Check the
    // ones that have been selected. Otherwise don't check any to allow all.
    if ($current_type) {
      $entity_type = $this->entityTypeManager
        ->getDefinition($current_type);
      if ($entity_type
        ->hasKey('bundle')) {

        // If all bundles have been allowed, don't select any.
        $enabled_all = array_diff(array_keys($extra_field_bundle_options), $sharemessage
          ->getExtraFieldBundles());

        // Get the selected bundles.
        $extra_field_bundles = array_intersect($sharemessage
          ->getExtraFieldBundles(), array_keys($extra_field_bundle_options));
        $form['sharemessage_extra_field']['bundles'] = [
          '#type' => 'checkboxes',
          '#title' => $this
            ->getEntityBundleLabel($entity_type),
          '#description' => t("Per default this extra field is not visible. Go to the bundle's Manage display page to enable it. Select none to allow all @content_entity.", [
            '@content_entity' => strtolower($this
              ->getEntityBundleLabel($entity_type)),
          ]),
          '#options' => $extra_field_bundle_options,
          '#default_value' => empty($enabled_all) ? [] : $extra_field_bundles,
          '#tree' => TRUE,
          '#open' => TRUE,
        ];
      }
    }

    // Update sharemessage_token_help according to the selected entity type.
    if ($this->moduleHandler
      ->moduleExists('token')) {

      // If '- None -' option is selected, show 'node' as default.
      $token_type = $current_type ? \Drupal::service('token.entity_mapper')
        ->getTokenTypeForEntityType($current_type) : 'node';
      $form['sharemessage_extra_field']['sharemessage_token_help'] = [
        '#title' => t('Replacement patterns'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#description' => t('These tokens can be used in all text fields.'),
        '#weight' => 2,
      ];
      $form['sharemessage_extra_field']['sharemessage_token_help']['browser'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => [
          $token_type,
          'sharemessage',
        ],
      ];
    }
    return $form;
  }

  /**
   * Ajax callback to fetch the selected Share Message settings.
   *
   * @param array $form
   *   A nested array form elements comprising the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function ajaxShareMessagePluginSelect(array $form, FormStateInterface $form_state) {
    return $form['plugin_wrapper'];
  }

  /**
   * Ajax callback to fetch the selected content type with view builder.
   *
   * @param array $form
   *   A nested array form elements comprising the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function ajaxShareMessageContentTypeSelect(array $form, FormStateInterface $form_state) {
    return $form['sharemessage_extra_field'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
    $sharemessage = parent::buildEntity($form, $form_state);
    if (!$sharemessage
      ->getSetting('override_default_settings')) {
      $sharemessage->settings = [];
    }

    // Store the selected content entities where the Share Message will be
    // displayed as an extra field into the config entity. Otherwise unset
    // extra_field_bundles if no specific entity type has been selected.
    $sharemessage
      ->setExtraFieldEntityType($form_state
      ->getValue('entity_type'));
    $sharemessage
      ->setExtraFieldBundles($form_state
      ->hasValue('bundles') ? array_keys(array_filter($form_state
      ->getValue('bundles'))) : []);

    // Move the override field into the settings array.
    //    if (\Drupal::config('sharemessage.settings')->get('message_enforcement')) {
    //      $sharemessage->settings['enforce_usage'] = $sharemessage->enforce_usage;
    //      unset($sharemessage->enforce_usage);
    //    }
    return $sharemessage;
  }

  /**
   * Provides the bundle label with a fallback when not defined.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type we are looking the bundle label for.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The entity bundle label or a fallback label.
   */
  protected function getEntityBundleLabel($entity_type) {
    if ($label = $entity_type
      ->getBundleLabel()) {
      return $this
        ->t('@label', [
        '@label' => $label,
      ]);
    }
    $fallback = $entity_type
      ->getLabel();
    if ($bundle_entity_type = $entity_type
      ->getBundleEntityType()) {

      // This is a better fallback.
      $fallback = $this->entityTypeManager
        ->getDefinition($bundle_entity_type)
        ->getLabel();
    }
    return $this
      ->t('@label bundle', [
      '@label' => $fallback,
    ]);
  }

  /**
   * Gets the bundles of the selected content entity type.
   *
   * @param string $entity_type
   *   The entity type where to retrieve the bundles.
   *
   * @return array
   *   An associative array of bundle's labels keyed by the bundle's ID.
   */
  protected function getBundles($entity_type) {
    $extra_field_bundle_options = [];
    foreach ($this->entityTypeBundleInfo
      ->getBundleInfo($entity_type) as $bundle => $info) {
      $extra_field_bundle_options[$bundle] = $info['label'];
    }
    return $extra_field_bundle_options;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
    $sharemessage = $this->entity;
    $sharemessage
      ->getPlugin()
      ->validateConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
    $sharemessage = $this->entity;
    $status = $sharemessage
      ->save();
    if ($status == SAVED_UPDATED) {
      $this
        ->messenger()
        ->addMessage(t('Share Message %label has been updated.', [
        '%label' => $sharemessage
          ->label(),
      ]));
    }
    else {
      $this
        ->messenger()
        ->addMessage(t('Share Message %label has been added.', [
        '%label' => $sharemessage
          ->label(),
      ]));
    }

    // Share Message settings might have changed, but it is not immediately
    // updated for the view display. Thus clear the entity extra field caches.
    \Drupal::service('entity_field.manager')
      ->clearCachedFieldDefinitions();
    $form_state
      ->setRedirect('entity.sharemessage.collection');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actions protected function Returns an array of supported actions for the current entity form. 29
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides FormInterface::submitForm 17
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
ShareMessageForm::$entityTypeBundleInfo protected property The entity type bundle info.
ShareMessageForm::$entityTypeManager protected property The entity type manager. Overrides EntityForm::$entityTypeManager
ShareMessageForm::$moduleHandler protected property The module handler. Overrides EntityForm::$moduleHandler
ShareMessageForm::$sharePluginManager protected property Share plugin manager.
ShareMessageForm::ajaxShareMessageContentTypeSelect public function Ajax callback to fetch the selected content type with view builder.
ShareMessageForm::ajaxShareMessagePluginSelect public function Ajax callback to fetch the selected Share Message settings.
ShareMessageForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityForm::buildEntity
ShareMessageForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ShareMessageForm::form public function Overrides Drupal\Core\Entity\EntityFormController::form(). Overrides EntityForm::form
ShareMessageForm::getBundles protected function Gets the bundles of the selected content entity type.
ShareMessageForm::getEntityBundleLabel protected function Provides the bundle label with a fallback when not defined.
ShareMessageForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
ShareMessageForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ShareMessageForm::__construct public function Constructs a new ShareMessageForm object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.