Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field from Slug should also check reverseSlug #569

Open
konstantinbrazhnik opened this issue Jun 17, 2022 · 1 comment
Open

Field from Slug should also check reverseSlug #569

konstantinbrazhnik opened this issue Jun 17, 2022 · 1 comment
Labels

Comments

@konstantinbrazhnik
Copy link

What version of the package/plugin are you using
v0.18.0

If at all possible, an isolated way to reproduce the behavior
use the \WPE\AtlasContentModeler\API\get_relationship function to lookup a reverse relationship. So Post_Type_A has a declared relationship to Post_Type_B with reverse relationship turned on. Try running get_relationship( $post_type_b_id, $relationship_name ) and you will get an error saying Content model field not found because the get_field_from_slug() function only checks for the model fields' slugs instead of slug and reverseSlug

if ( $field['slug'] === $slug ) {

The behavior you expect to see, and the actual behavior
The get_field_from_slug() function should be able to gracefully identify reverse relationships.

Probably something as simple as:

function get_field_from_slug( string $slug, array $models, string $post_type ): array {
	$models = append_reverse_relationship_fields( $models, $post_type );
	$fields = $models[ $post_type ]['fields'] ?? [];

	foreach ( $fields as $field ) {
		if ( $field['slug'] === $slug  || $field['reverseSlug'] === $slug ) {
			return $field;
		}
	}

	return [];
}
@nickcernis
Copy link
Member

nickcernis commented Jun 20, 2022

Thanks for the report, @konstantinbrazhnik!

Just confirming I can reproduce this. get_relationship will give the relationship when passed the field API ID (the ID from A->B), but not when given the field's reverse ID (from B->A).

Results from two models, AAA and BBB when passing a 'B' entry ID (2624) to get_relationship ('bBBs' is the relationship field ID, 'aAAs' is the reverse ID):

wp> \WPE\AtlasContentModeler\API\get_relationship( 2624, 'bBBs' );
=> phar:///usr/local/Cellar/wp-cli/2.6.0/bin/wp/vendor/wp-cli/shell-command/src/WP_CLI/Shell/REPL.php:52:
class WPE\AtlasContentModeler\ContentConnect\Relationships\PostToPost#2223 (12) {
  public $from =>
  string(3) "aaa"
  public $to =>
  array(1) {
    [0] =>
    string(3) "bbb"
  }
  public $name =>
  string(13) "1655733487943"
  public $id =>
  string(85) "wpe\atlascontentmodeler\contentconnect\relationships\posttopost-1655733487943-aaa-bbb"
  public $enable_from_ui =>
  bool(true)
  public $enable_to_ui =>
  bool(false)
  public $from_labels =>
  array(1) {
    'name' =>
    string(4) "BBBs"
  }
  public $to_labels =>
  array(1) {
    'name' =>
    string(13) "1655733487943"
  }
  public $from_sortable =>
  bool(false)
  public $to_sortable =>
  bool(false)
  public $is_bidirectional =>
  bool(true)
  public $cardinality =>
  string(10) "one-to-one"
}
wp> \WPE\AtlasContentModeler\API\get_relationship( 2624, 'aAAs' );
=> phar:///usr/local/Cellar/wp-cli/2.6.0/bin/wp/vendor/wp-cli/shell-command/src/WP_CLI/Shell/REPL.php:52:
class WP_Error#2336 (3) {
  public $errors =>
  array(1) {
    'field_not_found' =>
    array(1) {
      [0] =>
      string(29) "Content model field not found"
    }
  }
  public $error_data =>
  array(0) {
  }
  protected $additional_data =>
  array(0) {
  }
}

Screenshot 2022-06-20 at 16 47 59

(Internal team ref: https://wpengine.atlassian.net/browse/MTKA-1552. )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants