atlas-content-modeler icon indicating copy to clipboard operation
atlas-content-modeler copied to clipboard

Field from Slug should also check reverseSlug

Open konstantinbrazhnik opened this issue 2 years ago • 1 comments

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

https://github.com/wpengine/atlas-content-modeler/blob/aa21229009af8e6a2220aa0f6776139f9df864d8/includes/publisher/lib/field-functions.php#L70

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 [];
}

konstantinbrazhnik avatar Jun 17 '22 21:06 konstantinbrazhnik

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. )

nickcernis avatar Jun 20 '22 14:06 nickcernis