Fatal error following instructions

Created on 11 June 2024, 5 months ago
Updated 6 August 2024, 3 months ago

Problem/Motivation

Looking to add the module to my website so when content editors create new content they can choose to post to twitter (x)

Following the instructions we see the fatal errror like:

Fatal error: Declaration of Drupal\social_post_x\Plugin\Network\XPost::initSdk() must be compatible with Drupal\social_api\Plugin\NetworkBase::initSdk(): mixed in /opt/drupal/web/modules/social_post_x/src/Plugin/Network/XPost.php on line 30

Steps to reproduce

1. Install the required modules

2. Enable the modules

  • Workflows
  • Rules

3. Install this module

Rules Configuration

  1. Navigate to Configuration > Rules (/admin/config/workflow/rules) and click "Add reaction rule".
  2. Select a React on event value (e.g. "After saving a new content node").
  3. Set a Label for the rule (e.g. "New content node tweet").
  4. Click "Save".
  5. On the rule edit page, click "Add action".
  6. Select the "X Tweet" action under the "Social Post" option group in the Action field.
  7. Click "Continue".
  8. Input the text to be tweeted -- e.g. A new article "{{ node.title.value }}" has been published on my website at {{ node | entity_url }}
  9. Click "Save".
  10. Add other conditions and/or actions as desired.
  11. Click "Save".

On step 7. we see the Fatal error above.

Proposed resolution

Looks like there may be an issue on line 30 in XPost.php?

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom dave_______1

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dave_______1
  • 🇬🇧United Kingdom 2dareis2do

    Hi Dave

    Thanks for posting this issue.

    I have this working locally but I am using with

                "name": "drupal/social_api",
                "version": "3.1.0",
    

    I have not really looked into this but it sounds like something may have broken this in social_api 4.x

  • 🇬🇧United Kingdom 2dareis2do

    It looks like the signature has changed on 4.x branch. I n particular sdk is a mixed type. mixed type was introduced in php 8.0. Probably this needs updating so that the child class has the same.

    e.g.

    3.x

     /**
       * Sets the underlying SDK library.
       *
       * @return mixed
       *   The initialized 3rd party library instance.
       *
       * @throws \Drupal\social_api\SocialApiException
       *   If the SDK library does not exist or other validation fails.
       */
      abstract protected function initSdk(): mixed;
    
    

    4.x

      /**
       * The SDK client.
       *
       * @var mixed
       */
      protected mixed $sdk = NULL;
    

    I think the quick fix (and to check this) would be to make sure you are using social post 3.x

    e.g. in your project require

    "drupal/social_post": "^3.0",

    1. Probably need to specify a version of social_post that is compatible currently 3.x
    2. Test with 4.x version and a make sure signature is the same
    3. Make 4.x compatible

  • 🇬🇧United Kingdom 2dareis2do

    Also created 1.0.0 release

  • 🇬🇧United Kingdom 2dareis2do

    Sorry previous comments about the signature were incorrect. Please ignore.

    One thing I was wondering is how you managed to install social_api 4.x as from what I can see social_post has dependency of 3.x?

    The latest version of social_post is still 3.x afict

    https://git.drupalcode.org/project/social_post/-/blob/3.x/composer.json?...

  • 🇬🇧United Kingdom 2dareis2do

    Ok so for social api 4.x we have social_api/src/Plugin/NetworkInterface.php

    https://git.drupalcode.org/project/social_api/-/blob/4.0.x/src/Plugin/Ne...

    <?php
    
    namespace Drupal\social_api\Plugin;
    
    use Drupal\Component\Plugin\PluginInspectionInterface;
    use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
    
    /**
     * Defines an interface for Social Network plugins.
     */
    interface NetworkInterface extends PluginInspectionInterface, ContainerFactoryPluginInterface {
    
      /**
       * Gets the underlying SDK library.
       *
       * @return mixed
       *   The SDK client.
       */
      public function getSdk(): mixed;
    
    }
    

    where as in 3.x branch it is just:

    <?php
    
    namespace Drupal\social_api\Plugin;
    
    use Drupal\Component\Plugin\PluginInspectionInterface;
    use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
    
    /**
     * Defines an interface for Social Network plugins.
     */
    interface NetworkInterface extends PluginInspectionInterface, ContainerFactoryPluginInterface {
    
      /**
       * Gets the underlying SDK library.
       *
       * @return mixed
       *   The SDK client.
       */
      public function getSdk();
    
    }
    
    

    Notice how the signature has changed

    in

    we have

      /**
       * {@inheritdoc}
       */
      protected function initSdk() {
        $class_name = '\Abraham\TwitterOAuth\TwitterOAuth';
        if (!class_exists($class_name)) {
          throw new SocialApiException(sprintf('The PHP SDK for X could not be found. Class: %s.', $class_name));
        }
    
        /** @var \Drupal\social_post_x\Settings\XPostSettings $settings */
        $settings = $this->settings;
    
        retu

    so perhaps this need to be updated so that we have

    protected function initSdk(): mixed {

    That said I am still not sure why you have social_api 4.x installed?

  • 🇬🇧United Kingdom 2dareis2do

    Ok so it looks like social_post 4.x that is compatible with social_api 4.x is in the works.

    https://www.drupal.org/project/social_post/issues/3307661 💬 Support Social API v4 RTBC

    Perhaps it makes sense to add a a new branch/tag that supports that?

  • 🇬🇧United Kingdom dave_______1

    Thanks for looking into this. That signature mixed is a bit woolly and doesn't make a whole lot of difference, but I'd expect a change to the actual return value in the future. Might just be to pass linting rules...

    To get around this I added a change in workflow, the editor now creates content, and when happy, uses the share button on the Front End to post to different social networks.
    However, this workflow does not work for the scheduled content, which needs an automated approach like yours, so I'll need to add a ticket to try again.

    FYI I try not to use composer for the modules, I download and install manually. I've had problems with module dependencies and changes in the past, so when I get a selection that works together I add them to the repo. I can then duplicate the site with modules for different clients. If I need to update, I can do it locally and test everything before I go to prod. Symfony, Laravel or other PHP project I use Composer

Production build 0.71.5 2024