Skip to content

Commit 0220a95

Browse files
committed
Merge polylang support (see pr devgeniem#113)
2 parents 2fc21a3 + b190a38 commit 0220a95

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Field.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55

66
namespace Geniem\ACF;
77

8+
use Geniem\ACF\Field\Common\Translatable;
9+
810
/**
911
* Class Field
1012
*/
1113
abstract class Field {
14+
15+
/**
16+
* Import the translatable functionalities
17+
*/
18+
use Translatable;
19+
1220
/**
1321
* Field label.
1422
*

src/Field/Common/Translatable.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* ACF Codifier Translatable trait
4+
*/
5+
6+
namespace Geniem\ACF\Field\Common;
7+
8+
/**
9+
* Translatable trait
10+
*/
11+
trait Translatable {
12+
/**
13+
* The field translations value
14+
*
15+
* @var string
16+
*/
17+
protected $translations;
18+
19+
/**
20+
* Set field translations value
21+
*
22+
* @see https://polylang.pro/doc/working-with-acf-pro/#customize-acf-fields
23+
*
24+
* @throws \Geniem\ACF\Exception Throws error if translations is not valid.
25+
* @param string $translations The translations value to set.
26+
* @return self
27+
*/
28+
public function set_translations( string $translations ) {
29+
$category = \acf_get_field_type_prop( $this->type, 'category' );
30+
if ( 'layout' === $category ) {
31+
throw new \Geniem\ACF\Exception( self::class . ': set_translations() can\'t be called on this field type.' );
32+
}
33+
34+
$choices = [ 'ignore', 'copy_once', 'sync' ];
35+
switch ( $this->type ) {
36+
case 'text':
37+
case 'textarea':
38+
case 'wysiwyg':
39+
case 'oembed':
40+
case 'url':
41+
case 'email':
42+
$choices[] = 'translate';
43+
break;
44+
}
45+
46+
if ( ! \in_array( $translations, $choices ) ) {
47+
throw new \Geniem\ACF\Exception( self::class . ': set_translations() does not accept argument "' . $translations . '"' );
48+
}
49+
50+
$this->translations = $translations;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* Get the field translations value
57+
*
58+
* @return string
59+
*/
60+
public function get_translations() {
61+
return $this->translations;
62+
}
63+
}

0 commit comments

Comments
 (0)